• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# C++ Macro Problem

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

14 replies to this topic

Posted 08 June 2011 - 07:25 PM

Hi guys, check this out:

struct float2
{
float x;
float y;
};

#define set2(v,_x,_y) {v->x=_x; v->y=_y;}

// the following won't work because the curly braces don't get expanded:

if(a == 1)
set2(&v,1.0f,1.0f);
else
set2(&v,0.0f,0.0f);

// illegal else without matching if

I know I can put the curly braces in the conditional but is there any way I can get it to work as is ?

Thanks.

### #2kloffy  Members

Posted 08 June 2011 - 07:34 PM

I know I can put the curly braces in the conditional but is there any way I can get it to work as is ?

There's no better way than using a trivial loop or a conditional. See What should be done with macros that have multiple lines?

### #3scgames  Members

Posted 08 June 2011 - 07:51 PM

Any reason you're not just using a function?

### #4Hodgman  Moderators

Posted 08 June 2011 - 08:04 PM

Expand you macro and look at what's produced:
if (a == 1)
{&v->x= 1.0f; &v->y=1.0f;}
;
else
{&v->x= 0.0f; &v->y=0.0f;}
;
Your problem has nothing to do with braces -- the problem is semi-colons.

But as jyk said above, there's no reason to use a #define for this when an inline function will suffice.

### #5wqking  Members

Posted 08 June 2011 - 08:14 PM

One more thing, VC can (maybe GCC also can) generate .i files which is expanded by preprocessor, so you can see how the macro is expanded.

But more important, be careful to use macro, only use macro for magic things. Always prefer functions and constants to macros.

http://www.cpgf.org/
cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.
v1.5.5 was released. Now supports tween and timeline for ease animation.

### #6Steno  Members

Posted 08 June 2011 - 08:25 PM

Any reason you're not just using a function?

I'm guessing it's a theoretical question. The practical reason is if you needed to inline functions in older (e.g. C89 and K&R C) code.

### #7scgames  Members

Posted 08 June 2011 - 08:32 PM

I'm guessing it's a theoretical question. The practical reason is if you needed to inline functions in older (e.g. C89 and K&R C) code.

Only the OP can tell us for sure, but I get the impression the question is practical in nature. For one thing, the thread title is 'C++ Macro Problem', which suggests the question is specifically in reference to C++ rather than C.

I could be wrong though (maybe the OP will clarify).

Posted 10 June 2011 - 04:20 PM

Hi guys,

It's practical in nature. I read in a few places that inline isn't guaranteed so I thought macros for these simple operations would be better. I'm starting to think I should be using funtions though.

Thanks for all the info.

PS - I'm using VS2008

### #9SiCrane  Moderators

Posted 10 June 2011 - 05:36 PM

With MSVC you can use __forceinline which will either inline the function or generate a level 1 warning when the function can't be inlined.

### #10Aardvajk  Members

Posted 11 June 2011 - 03:49 AM

[source lang="cpp"]struct float2{ float x,y; void set(float a,float b){ x=a; y=b; }};[/source]

Declaring and defining the method within the class automatically implies inline. If you are optimizing for speed in your release build, it is a far better idea normally to allow the compiler to decide whether to inline or not, unless you have profiled and proven a bottleneck.

### #11rip-off  Moderators

Posted 11 June 2011 - 04:57 AM

It's practical in nature. I read in a few places that inline isn't guaranteed so I thought macros for these simple operations would be better.

Just because it isn't guaranteed doesn't mean it isn't happening. Such a trivial function will almost certainly be inlined.

As Asrdvajk says, only profiling can indicate if the decision to inline or not is justified. Remember than forcing inlining can cause code bloat, which can slow things down instead of speeding them up.

### #12wqking  Members

Posted 11 June 2011 - 07:55 AM

One more thing, VC can (maybe GCC also can) generate .i files which is expanded by preprocessor, so you can see how the macro is expanded.

But more important, be careful to use macro, only use macro for magic things. Always prefer functions and constants to macros.

Just curious, which word is wrong to get down vote?
Voting worth nothing but unreasonable down vote is not friendly.

http://www.cpgf.org/
cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.
v1.5.5 was released. Now supports tween and timeline for ease animation.

### #13Ezbez  Members

Posted 11 June 2011 - 08:06 AM

Just curious, which word is wrong to get down vote?
Voting worth nothing but unreasonable down vote is not friendly.

One errant down vote shouldn't be taken too seriously. I would *guess* that it was your last line: "But more important, be careful to use macro, only use macro for magic things. Always prefer functions and constants to macros." This seems to support using macros for bizarre, unusual, unreadable, and surprising code. Generally the better advice is to avoid anything "magical" in the first place. However, I think you were just trying to say to use them when the only alternatives are going to be really messy.

BTW, I gave you an upvote since your .i file suggestion is a good one.

### #14wqking  Members

Posted 11 June 2011 - 08:20 AM

One errant down vote shouldn't be taken too seriously. I would *guess* that it was your last line: "But more important, be careful to use macro, only use macro for magic things.

Macro can do magic things for very useful cases, one case is simulating variadic template parameters. Boost library uses it. Boost also used macro extensively for some magic things which the library users will never aware.
Carefully using macro will really get very amazing powerful use. I used macros to make a callback system.
That's I called magic.
Functions and constants are not magic, so don't use macros for them.

BTW, I gave you an upvote since your .i file suggestion is a good one.

Thanks.
I really hope gdnet won't go stackoverflow way, full of up/down vote and a lot of votes are quite bias (GCC fans may down vote any one recommending VC, etc) and subjective. (no argument on this point because it's off topic).

Edited:
Again i don't mind down vote, but please jump out to give the reason, thanks.

http://www.cpgf.org/
cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.
v1.5.5 was released. Now supports tween and timeline for ease animation.

### #15Aardvajk  Members

Posted 11 June 2011 - 09:47 AM

Downvotes corrected. No idea what that was about. We are all aware that there are certain things you can only do with macros in C++.

My personal rule of thumb is: If it can be done without a macro, do it the other way, otherwise suck it up or move to a more modern language.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.