va-args not resolving correctly
I have the following function:
void Register(Data* _pData, uint32 _Line, const char* _pFile, ...)
{
va_list Args;
va_start(Args, _pFile);
for(uint i = 0;i m_NumFloats; ++i)
{
_pData->m_Floats[i] = va_arg(Args, fp32);
}
va_end(Args);
}
Which is called by the macro:
#define REG(_Name, ...)\
{\
if(s_##_Name##_Data.m_Enabled)
Register(&s_##_Name##_Data, __LINE__, __FILE__, ##__VA_ARGS__);\
}\
With the usage:
REG(Test, (fp32)0.42f);
The Data-struct looks like:
struct Data
{
int m_NumFloats;
fp32 m_Floats[开发者_JS百科4];
}
The creation-macro of Data creates the static Data g_YourName_Data
and initializes it correctly with a maximum of 4 m_NumFloats.
The va_arg call resolves to 0.0. s_Test_Data exists and the Register-function is called appropriate. va-list just simply won't let me resolve the first argument into the float that I passed it into. Is there anything specific that I'm missing?
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.
精彩评论