Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #10845

a kind of template (or macro) to reuse parts of code...

From "A" <a@a.a>
Newsgroups comp.lang.c++
Subject a kind of template (or macro) to reuse parts of code...
Date 2011-10-14 21:25 +0200
Organization B.net Hrvatska d.o.o.
Message-ID <j7a2dr$reb$1@gregory.bnet.hr> (permalink)

Show all headers | View raw


Can this be done (and how) in C++ in the following pseudo-code form:

DEFINE_I_TO_1
{
int i=1;
}

int increaseby1()
{
DEFINE_I_TO_1
return i+1;
}

int increaseby2()
{
DEFINE_I_TO_1
return i+2;
}

In other words - I would like to keep all the scopes, definitions etc and 
just to insert a reusable piece of code - much like copy-pasting a block of 
template code.

Of course, I could put it in a function, but I would like to know also if 
there is a way to do it this way because in certain uses this would make 
sense - like loops where timing is critical putting inline code would 
obviously be faster way than calling a function.

I know C++ has macros and templates but from what I've seen they are not 
intended to be used like this. Or am I wrong?

Is this possible and what is the easiest way to achieve this if possible? 

Back to comp.lang.c++ | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

a kind of template (or macro) to reuse parts of code... "A" <a@a.a> - 2011-10-14 21:25 +0200
  Re: a kind of template (or macro) to reuse parts of code... "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2011-10-14 22:08 +0200
    Re: a kind of template (or macro) to reuse parts of code... "A" <a@a.a> - 2011-10-14 22:16 +0200
      Re: a kind of template (or macro) to reuse parts of code... Ian Collins <ian-news@hotmail.com> - 2011-10-15 09:22 +1300
        Re: a kind of template (or macro) to reuse parts of code... "A" <a@a.a> - 2011-10-14 22:36 +0200
          Re: a kind of template (or macro) to reuse parts of code... Ian Collins <ian-news@hotmail.com> - 2011-10-15 09:53 +1300
            Re: a kind of template (or macro) to reuse parts of code... "A" <a@a.a> - 2011-10-15 16:22 +0200
              Re: a kind of template (or macro) to reuse parts of code... Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-10-15 16:54 +0000
          Re: a kind of template (or macro) to reuse parts of code... Noah Roberts <roberts.noah@gmail.com> - 2011-10-14 14:07 -0700
      Re: a kind of template (or macro) to reuse parts of code... Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-10-15 07:33 +0000
    Re: a kind of template (or macro) to reuse parts of code... "A" <a@a.a> - 2011-10-14 22:18 +0200
  Re: a kind of template (or macro) to reuse parts of code... Marcel Müller <news.5.maazl@spamgourmet.com> - 2011-10-14 23:01 +0200
  Re: a kind of template (or macro) to reuse parts of code... Richard Damon <news.x.richarddamon@xoxy.net> - 2011-10-14 17:31 -0400
    Re: a kind of template (or macro) to reuse parts of code... "A" <a@a.a> - 2011-10-15 16:13 +0200
      Re: a kind of template (or macro) to reuse parts of code... "A" <a@a.a> - 2011-10-15 17:54 +0200
        Re: a kind of template (or macro) to reuse parts of code... Richard Damon <news.x.richarddamon@xoxy.net> - 2011-10-15 12:02 -0400

csiph-web