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.
C++ Macro Problem
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?
Expand you macro and look at what's produced:
But as jyk said above, there's no reason to use a [font="Courier New"]#define[/font] for this when an [font="Courier New"]inline[/font] function will suffice.
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 [font="Courier New"]#define[/font] for this when an [font="Courier New"]inline[/font] function will suffice.
Others had given the answer.
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.
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.
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.
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).
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
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
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.
[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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement