tags:

views:

70

answers:

1
+2  A: 

Try:

#define REG(_Name, ...)\
{\
   if(s_##_Name_Data.m_Enabled)\
      Register(&s_##_Name_Data, __LINE__, __FILE__, __VA_ARGS__);\
}

Get rid of the token-pasting operator. You we're also missing a '\' in your macro (maybe a copy-n-paste error?).

Also, use va_arg(), not va_args(). And I'm not sure if you meant for _Name to be _Name_Data or the other way around.

Finally, I assumed that fp32 was an alias for float; GCC had this to say to me:

C:\TEMP\test.c:22: warning: `fp32' is promoted to `double' when passed through `...'
C:\TEMP\test.c:22: warning: (so you should pass `double' not `fp32' to `va_arg')
C:\TEMP\test.c:22: note: if this code is reached, the program will abort

You should heed that warning. The program does crash for me if I do not.

Michael Burr
The `, ## __VA_ARGS__` thing is a GCC extension which makes the comma go away if `__VA_ARGS__` expands to no tokens (i.e. if REG() is called with only one argument). In *this case* it's inappropriate because Register() must receive at least one anonymous argument, and you'd like that to be a compile error rather than garbage at runtime, but it's not automatically wrong.
Zack
@Zack - thanks for that information. I knew the GCC had a way to deal with empty `__VAR_ARGS__`, but didn't remember that that was it.
Michael Burr
Thanks for the answer Michael, and sorry for the copy-and-paste errors. I'll edit the post to make it correct. I'm not getting any such warnings. The real code handles the case where no va-args are present, the number of arguments expected are defined earlier.Should you really append an extra `\ ` on the last line of the macro? I'm quite certain that you don't need that (since I have several macros which don't have it and it would have caused some compiler errors if it didn't). `_Name` is inserted into the identifier that we're accessing. In this case the identifier would become `s_Test_Data`.
Simon
Oh, I'm using microsoft's compiler and not GCC.
Simon
Changing it into reading a `double` made it work though! Thanks a ton!
Simon
You definitely don't want a backslash on the last line of the macro, it'll suck whatever's on the *next* line of source into the macro as well. Probably right now that's a blank line and so it's harmless, but then someone comes along and decides that the code would look better without the blank line and hilarity ensues.
Zack
@Simon: "I'm using Microsoft's compiler and not GCC" - I was really, really surprised that GCC diagnosed that problem in a warning. I doubt that it's something I would have figured out on my (at least not without quite a debugging effort). This is one reason I try to compile snippets with problems using several compilers. Kudos to GCC for diagnosing that.
Michael Burr
@Zack - good catch on the trailing backslash...
Michael Burr
@Michael: Indeed, GCC tend to warn about a lot more than MSVC so I should probably try to set up my build-system to work with GCC as well just to catch more errors. Thanks again!
Simon