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

A

A

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

Alf P. Steinbach

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
 
A

A

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...
 
A

A

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.
 
I

Ian Collins

On 10/15/11 09:16 AM, A wrote:

Please don't snip attributions, it is rude.
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.
 
A

A

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

Ian Collins

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:

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.
 
M

Marcel Müller

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
 
N

Noah Roberts

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.
 
R

Richard Damon

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;
}
 
J

Jorgen Grahn

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
 
A

A

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

A

*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.

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.
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.
 
A

A

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; \
}

#define CODEPART2 \
return abc[4];

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

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

Richard Damon

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; \
}

#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.
 
J

Jorgen Grahn

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top