C ++ Extract std :: string from variable length argument list

Hello to all! I'm trying to make a simple copy of sprintf that returns a formatted string, but I get into a small clue ...

Apparently, using a variable-length argument list, you cannot pass an instance of std :: string.

I already have a parser that works correctly with int, double, float, char, const char *, char * ... I still need to get the lines to work.: \

If you're interested, this is a compilation error that I get: /root/learncpp/StringFormat/main.cpp:8: warning: cannot pass objects of non-POD type 'struct std::string' through '...'; call will abort at runtime /root/learncpp/StringFormat/main.cpp:8: warning: cannot pass objects of non-POD type 'struct std::string' through '...'; call will abort at runtime

The main reason I do this is that I can have convenient formatting without relying on third-party libraries, but still I don't need to add the ".c_str ()" line to every instance that I use.

Help with this will be greatly appreciated. Perhaps there is another version of variable length argument lists made specifically for C ++?

EDIT: I just realized if you pass a pointer to a string (e.g. using a prefix and a prefix), it works well. All you have to do is expand the pointer to a string in user sprintf by passing the address std :: string!

However, it would be nice to see if there is a way to support the string directly through variable-length argument lists. Thanks!

+4
source share
1 answer

No - as the compiler said, you are allowed to pass objects of type POD to a variable function.

What you usually want to do is to exclude the use of a function variable in the first place, for example, using iostream instead of something like printf (or stringstream instead of sprintf).

+3
source

All Articles