I’m trying to use va_arg to make a generic factory function in my GUI library.
When passing va_arg twice in the same function they pass on the same value instead of two different:
GUIObject* factory(enumGUIType type, GUIObject* parent, ...){
va_list vl;
va_start(vl, parent);
...
label->SetPosition(va_arg(vl, int), va_arg(vl, int));
va_end(vl);
return finalObjectPointer;
}
factory(LABEL, theParent, 100,200); // Results in position 200:200
What causes this unexpected behavior?
The compiler is not guaranteed to evaluate arguments in order. Add some additional local variables and do the two assignments in sequence.
See this other stack overflow posting.
To get what you are observing: the exact same value twice — probably requires a compiler bug piled on top of the undefined order of evaluation situation, or some entertaining aspect of the particular macro expansion of va_arg in your environment.