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


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

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

Started by"A" <a@a.a>
First post2011-10-14 21:25 +0200
Last post2011-10-15 12:02 -0400
Articles 16 — 7 participants

Back to article view | Back to comp.lang.c++


Contents

  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

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

From"A" <a@a.a>
Date2011-10-14 21:25 +0200
Subjecta kind of template (or macro) to reuse parts of code...
Message-ID<j7a2dr$reb$1@gregory.bnet.hr>
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? 

[toc] | [next] | [standalone]


#10846

From"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>
Date2011-10-14 22:08 +0200
Message-ID<j7a4rj$g9k$1@dont-email.me>
In reply to#10845
On 14.10.2011 21:25, A wrote:
> 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?

The above sounds as if you're asking about how to define functions, but 
then in the text you indicate that you are familiar with that. So it 
does not make sense as given. You need to provide a more clear example.

Read the FAQ item on how to post question.

It is always a good idea to check the FAQ before asking.


Cheers & hth.,

- Alf

[toc] | [prev] | [next] | [standalone]


#10847

From"A" <a@a.a>
Date2011-10-14 22:16 +0200
Message-ID<j7a5ce$13h$1@gregory.bnet.hr>
In reply to#10846
> The above sounds as if you're asking about how to define functions, but 
> then in the text you indicate that you are familiar with that. So it does 
> not make sense as given. You need to provide a more clear example.

If I call a function that would imply after compilation - storing registers 
on stack, a call to function, then returning registers from stack and 
return. All of that takes CPU cycles for time-critical loops. With inline 
code it would be compilers job to kind of "paste" a code defined in the 
template. And no, I don't really want to use inline assembly.

If I modify the code in the template it is "pasted" on next compilation. 
Once, not 10 times in all 10 places where I use this let's call it macro or 
template.

So that's what I'm looking a way for to do. Like I said, I know how to use 
functions.

I hope it is more clear now... 

[toc] | [prev] | [next] | [standalone]


#10849

FromIan Collins <ian-news@hotmail.com>
Date2011-10-15 09:22 +1300
Message-ID<9frk0iFvblU1@mid.individual.net>
In reply to#10847
On 10/15/11 09:16 AM, A wrote:

Please don't snip attributions, it is rude.

>> The above sounds as if you're asking about how to define functions, but
>> then in the text you indicate that you are familiar with that. So it does
>> not make sense as given. You need to provide a more clear example.
>
> If I call a function that would imply after compilation - storing registers
> on stack, a call to function, then returning registers from stack and
> return. All of that takes CPU cycles for time-critical loops. With inline
> code it would be compilers job to kind of "paste" a code defined in the
> template. And no, I don't really want to use inline assembly.

How do you know?  Have you measured?

C++ (and C these days) compilers are very good at inlining trivial 
functions.

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#10850

From"A" <a@a.a>
Date2011-10-14 22:36 +0200
Message-ID<j7a6ip$3cr$1@gregory.bnet.hr>
In reply to#10849
> C++ (and C these days) compilers are very good at inlining trivial 
> functions.

OK, it is not just the speed, it is also the convenience and maintaining the 
code.

Anyway, back to topic, is something like what I asked possible in a way to 
do with macros or templates or any other way except functions?

[toc] | [prev] | [next] | [standalone]


#10851

FromIan Collins <ian-news@hotmail.com>
Date2011-10-15 09:53 +1300
Message-ID<9frlr7FvblU2@mid.individual.net>
In reply to#10850
On 10/15/11 09:36 AM, A wrote:

*Please don't snip attributions, it is rude.*  I'll ignore you if you do 
it again.

 > Ian wrote:

>> C++ (and C these days) compilers are very good at inlining trivial
>> functions.
>
> OK, it is not just the speed, it is also the convenience and maintaining the
> code.

How is a macro list thing more convenient than a function?

> Anyway, back to topic, is something like what I asked possible in a way to
> do with macros or templates or any other way except functions?

Why?  You haven't provided any justification for this new construct.

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#10860

From"A" <a@a.a>
Date2011-10-15 16:22 +0200
Message-ID<j7c503$j0g$1@gregory.bnet.hr>
In reply to#10851
> *Please don't snip attributions, it is rude.*  I'll ignore you if you do 
> it again.

Sorry, don't have idea what you're talking about here. Just cutting out the 
relevant part for the answer. All newsreaders have in-reference-to header so 
it is groupped as it should be.

>> How is a macro list thing more convenient than a function?

Because in this particular case it would be more useful. I have overloaded 
function that calls something like this:

- a block of always the same block of code here
- something different
- a different block of the same code here

and it is repeated in overloaded functions over and over. if I make a simple 
change in one or the other block I have to copy-paste them in each 
overloaded function. maintaining this would be easier if this code is placed 
in a function or... a kind of macro like i asked for in above explanations.

>> do with macros or templates or any other way except functions?
> Why?  You haven't provided any justification for this new construct.

I am informing myself of other ways of doing things. I already explained I 
can do it with functions but in this particular case a macro thing would be 
way more practical. No solution fits purposes. 

[toc] | [prev] | [next] | [standalone]


#10863

FromJorgen Grahn <grahn+nntp@snipabacken.se>
Date2011-10-15 16:54 +0000
Message-ID<slrnj9jemk.1bk.grahn+nntp@frailea.sa.invalid>
In reply to#10860
On Sat, 2011-10-15, A wrote:
>> *Please don't snip attributions, it is rude.*  I'll ignore you if you do 
>> it again.
>
> Sorry, don't have idea what you're talking about here.

What you just did -- skip the line "Ian Collins wrote:". It's that
thing which lets people look at your posting *only* and know who wrote
what.

The rest of your quoting seems fine to me.

/Jorgen

-- 
  // Jorgen Grahn <grahn@  Oo  o.   .     .
\X/     snipabacken.se>   O  o   .

[toc] | [prev] | [next] | [standalone]


#10853

FromNoah Roberts <roberts.noah@gmail.com>
Date2011-10-14 14:07 -0700
Message-ID<cdeec712-dd89-4fb0-9209-6f3f19bab393@p1g2000yqk.googlegroups.com>
In reply to#10850
On Oct 14, 1:36 pm, "A" <a...@a.a> wrote:
> > C++ (and C these days) compilers are very good at inlining trivial
> > functions.
>
> OK, it is not just the speed, it is also the convenience and maintaining the
> code.
>

Because coming up with obscure macros that hide variable scoping
issues is easier to maintain than functions.

[toc] | [prev] | [next] | [standalone]


#10857

FromJorgen Grahn <grahn+nntp@snipabacken.se>
Date2011-10-15 07:33 +0000
Message-ID<slrnj9idqv.1bk.grahn+nntp@frailea.sa.invalid>
In reply to#10847
On Fri, 2011-10-14, A wrote:
>> The above sounds as if you're asking about how to define functions, but 
>> then in the text you indicate that you are familiar with that. So it does 
>> not make sense as given. You need to provide a more clear example.
>
> If I call a function that would imply after compilation - storing registers 
> on stack, a call to function, then returning registers from stack and 
> return. All of that takes CPU cycles for time-critical loops. With inline 
> code it would be compilers job to kind of "paste" a code defined in the 
> template. And no, I don't really want to use inline assembly.

You use the term "inline" over and over, but you seem to overlook
inline functions.  All the (relatively) costly things you mention above,
plus the missed optimization opportunities, go away with inline
functions.

...
> So that's what I'm looking a way for to do. Like I said, I know how to use 
> functions.

Not if you equate calling a function with heavy stack/register
operations.

/Jorgen

-- 
  // Jorgen Grahn <grahn@  Oo  o.   .     .
\X/     snipabacken.se>   O  o   .

[toc] | [prev] | [next] | [standalone]


#10848

From"A" <a@a.a>
Date2011-10-14 22:18 +0200
Message-ID<j7a5hb$16v$1@gregory.bnet.hr>
In reply to#10846
> DEFINE_I_TO_1
> {
> int i=1;
> }
> int increaseby1()
> {
> DEFINE_I_TO_1
> return i+1;
> }

the capitals imply it is inline code and not a function call... see "i" is 
not defined in increaseby1() function, but the macro or whatever would 
define it in this pseudo-code case. 

[toc] | [prev] | [next] | [standalone]


#10852

FromMarcel Müller <news.5.maazl@spamgourmet.com>
Date2011-10-14 23:01 +0200
Message-ID<4e98a310$0$7608$9b4e6d93@newsspool1.arcor-online.net>
In reply to#10845
On 14.10.2011 21:25, A wrote:
> 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.

You should have more confidence in modern compilers. Sometimes you will 
not recognize your original code in the generated assembly.

C++11 improves things even more, e.g. with constexpr.

> 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?

There is nothing wrong with functions for reusable code.

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

Did you measure the predicated impact of inline functions in optimized 
builds?


Marcel

[toc] | [prev] | [next] | [standalone]


#10854

FromRichard Damon <news.x.richarddamon@xoxy.net>
Date2011-10-14 17:31 -0400
Message-ID<j7a9o8$g7k$1@dont-email.me>
In reply to#10845
On 10/14/11 3:25 PM, A wrote:
> 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?
>
>

First,
the way you have defined DEFINE_I_TO_1 it generates a variable i with a 
local scope that then immediately ends. Your following examples then 
will generate errors (unless there is another i out side the scope of 
these snippets

It does sound like you very much really want:

#define DEFINE_I_TO_1 int i=1;

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

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

[toc] | [prev] | [next] | [standalone]


#10859

From"A" <a@a.a>
Date2011-10-15 16:13 +0200
Message-ID<j7c4ds$hu3$1@gregory.bnet.hr>
In reply to#10854
> #define DEFINE_I_TO_1 int i=1;

so it is a macro... this looks like something what i had in mind

will something like this work for more than a single line of code? 

[toc] | [prev] | [next] | [standalone]


#10861

From"A" <a@a.a>
Date2011-10-15 17:54 +0200
Message-ID<j7cacb$sr9$1@gregory.bnet.hr>
In reply to#10859
I tried this (as an dumb example) and it seems to work...
I think I'll be learning a bit more about macros.

The only drawback I see so far is the fact I can't debug the CODEPART1 and 
CODEPART2 but except that it is working as intented.

#define CODEPART1                         \
std::vector<int> abc(5);                  \
for (int i = 0; i < abc.size(); i++)      \
{                                         \
abc[i] = i;                               \
}

#define CODEPART2                         \
return abc[4];

int overload(int a)
{
CODEPART1
abc[0] = 0;
CODEPART2
}

int overload(__int64 a)
{
CODEPART1
abc[1] = 0;
CODEPART2
}

[toc] | [prev] | [next] | [standalone]


#10862

FromRichard Damon <news.x.richarddamon@xoxy.net>
Date2011-10-15 12:02 -0400
Message-ID<j7caq9$311$1@dont-email.me>
In reply to#10861
On 10/15/11 11:54 AM, A wrote:
> I tried this (as an dumb example) and it seems to work...
> I think I'll be learning a bit more about macros.
>
> The only drawback I see so far is the fact I can't debug the CODEPART1 and
> CODEPART2 but except that it is working as intented.
>
> #define CODEPART1                         \
> std::vector<int>  abc(5);                  \
> for (int i = 0; i<  abc.size(); i++)      \
> {                                         \
> abc[i] = i;                               \
> }
>
> #define CODEPART2                         \
> return abc[4];
>
> int overload(int a)
> {
> CODEPART1
> abc[0] = 0;
> CODEPART2
> }
>
> int overload(__int64 a)
> {
> CODEPART1
> abc[1] = 0;
> CODEPART2
> }
>
>

The limitations on debugging are a function of the debugger. A good 
debugger will at least let you switch into an assembly view of the code 
to see what is happening.

If you are having trouble with a macro expansion, replacing the macro 
with its expanded text temporarily normally will make it easier to debug 
(and then if you needed to change the code, go back and fix the original 
macro to match, but ideally you would have debugged the snippet as 
independent code before pushing it into a macro.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web