mardi 27 juin 2017

Variable Argument List following 'const std::string&' messes up the stack

I have a function: std::string format(const std::string sformat, ...); It basically works like a fancy sprintf().

Since I call that function a lot I wanted to pass the format string as a ref: const std::string& sformat. Unfortunately va_start() seems to have problems finding the right spot on the stack to the variable argument list.

If I remove the '&' it works fine.

Two questions: A: Do I even need the reference in order to prevent a copy of sformat during the call, or is the optimizer clever enough to just do a pass-by-ref in the background? B: What can I do to prevent a pass-by-value of the format string and still don't confuse va_start() ?

My guess is: pass-by-pointer. Or is there a better solution?

Aucun commentaire:

Enregistrer un commentaire