variable argument list + std::string

Started by
14 comments, last by ToohrVyk 15 years, 9 months ago
Hello all! Im sorry about the post spam. My laptop totally went crazy and I got script errors and such from GameDev.net. The question is (as some people already figured out) how I could get around the fact that I cant use std::string with a variable argument list. Im using this list for a logfunction, and I would need it pretty bad. It works just like sprintf and it works like a charm except for one thing. I would like to pass a std::string variable to the list and then read it somehow. Is this possible?


//Function
void Log(std::string format, ...)
{
   //do stuff.
   
   char *p = va_arg(list, char*);
   
   log << p;

   //do some more stuff.
}

//Usage
Log("My name is %s", "Lucas"); //Works

std::string name = "Lucas";
Log("My name is %s", name); //Does obviously not work.


So is there any way to get around this? (Changing the variable to a char pointer is not an option). Thanks, and sorry for the mess. [Edited by - Lucas W on July 20, 2008 6:20:07 PM]
-----------------------------------------------------I demand more toasties!
Advertisement
So?
This has had 4 posts, I think somethings wrong with his browser/connection/mousemat

"The right, man, in the wrong, place, can make all the dif-fer-rence in the world..." - GMan, Half-Life 2

A blog of my SEGA Megadrive development adventures: http://www.bigevilcorporation.co.uk

I think we can guess the question, though. The question is probably why std::strings won't work via an ellipsis. The answer is that it has to do with the way variable argument lists work. The real fix to avoiding this problem is not using variable argument lists. Instead, using something like boost::format will make your life much easier.
The c_str() method returns a const char pointer, that can be passed into an ellipsis.

However, I agree that avoiding variable argument lists altogether is the better approach, because they are not type safe at all.
Quote:Original post by Rattenhirn
The c_str() method returns a const char pointer, that can be passed into an ellipsis.

However, I agree that avoiding variable argument lists altogether is the better approach, because they are not type safe at all.


I was a bit vague. I meant as part of the format string. You can't have the format string be a std::string. I was just guessing that that was his problem, since it happened to me, once, a while back. That was when I swore of using varargs -- too many risks. Other than, we're in agreement, which is good. [smile]
Please take a look again at my problem and tell me how I should solve this :)

What is this boost::format and how do I use it?
-----------------------------------------------------I demand more toasties!
template<typename T>Log &operator<<(const T &rhs){    // do stuff    log << rhs;    // do some more stuff    return *this;}std::string name = "Lucas";log << "my name is " << name;


Why are you using a C idiom in C++?
Quote:Original post by Lucas W
What is this boost::format and how do I use it?


Boost.Format

Google + Excellent Boost documentation online ftw.
[TheUnbeliever]
Quote:Original post by Lucas W
Please take a look again at my problem and tell me how I should solve this :)

What is this boost::format and how do I use it?


The parameter for the format string can't be a std::string or any C++ object. It has to be a raw string. So to be safe it should look like this:

//Functionvoid Log(const char* format, ...){   //do stuff.      char *p = va_arg(list, char*);      log << p;   //do some more stuff.}//UsageLog("My name is %s", "Lucas"); //Worksstd::string name = "Lucas";Log("My name is %s", name.c_str()); // Convert to raw string when passing in


But, as mentioned, you lose type safety. Another option is to do this:

//Functionvoid Log(const std::string& str){   //do stuff.      log << str;   //do some more stuff.}using boost::format;using boost::str;//UsageLog(str(format("My name is %s") % "Lucas")); //Worksstd::string name = "Lucas";Log(str(format("My name is %s") % name)); // Works with C++ string


Boost format gives you a more typesafe way to do your typical printf-like string operations. For more information, check here: http://www.boost.org/doc/libs/1_35_0/libs/format/doc/format.html

Finally, I did learn something new from this post. When I said the format string can't be a std::string, I believe I had tried this:

void Log(const std::string& format, ...){   ...}


Which cannot work. Hadn't occurred to me that passing by value would allow it to work. Either way, ultimately another nasty feature of var args. It's not important, though, since I refuse to use var args anymore, anyway. [smile]

This topic is closed to new replies.

Advertisement