std::String string1 = "Date= ";
int day = 1;
int month = 4;
int year = 2009;
void MyFunction( std::String );
//use it like so
MyFunction( string1 + day + " " + month + " , " + year );
// should pass in a string of "Date= 1 4, 2009"
Is this possible Function( (string1 + string2 + string3 + int) )
What I'm trying to do is combine things as a string at the point of passing them into a function.
example
Is this possible, maybe using a stringstream as the argument of using templates (I've never used them before) or some other way, that does require me to create the string first then just pass it in?
If I understand what you're asking:
std::stringstream sstr;sstr << string1 << day << " " << month << " , " << year;MyFunction(sstr.str());
Quote:Original post by SiCrane
If I understand what you're asking:std::stringstream sstr;sstr << string1 << day << " " << month << " , " << year;MyFunction(sstr.str());
thats what I want to accomplish but I want it in the function so the function can take any type of argument and any combonation but use it like a string.
is this possible
Myfunction( (std::stringstream ss() << string1 << int << string2).str() );
The stringstream is probably the best way, as mentioned. But you can choose to wrap up that functionality into a different interface if you want.
QT's QString can be used like so:
QString date("%1/%2/%3").arg(month).arg(day).arg(year);
My engine has a similar version which operates like so:
FormatString date("%%/%%/%%") % month % day % year;
Under the hood it's really all the same thing. Go with the stringstream.
QT's QString can be used like so:
QString date("%1/%2/%3").arg(month).arg(day).arg(year);
My engine has a similar version which operates like so:
FormatString date("%%/%%/%%") % month % day % year;
Under the hood it's really all the same thing. Go with the stringstream.
Quote:Original post by bzroom
MyFunction((std::stringstream()<<s1<<s2).str()) aught to work.
No, that requires binding an rvalue to a non-const reference, which isn't legal standard C++.
You could use boost::lexical_cast to convert values to strings and use string concatenation:
MyFunction( string1 + boost::lexical_cast<std::string>(day) + " " + boost::lexical_cast<std::string>(month) + " , " + boost::lexical_cast<std::string>(year) );
or you could overload the '+' operator and convert every possible data type to a string and do string concatenation for everything.
Quote:Original post by SiCraneOr you could use boost::format, which does that for you internally...
You could use boost::lexical_cast to convert values to strings and use string concatenation:
Quote:Original post by SiCraneQuote:Original post by bzroom
MyFunction((std::stringstream()<<s1<<s2).str()) aught to work.
No, that requires binding an rvalue to a non-const reference, which isn't legal standard C++.
std::stringstream::str() returns by value (to mutate the underlying string, you have to call an overload ::str(const std::string&)), so as long as the function accepts a value or a const reference, there should be no problem with the binding. (Unless I've missed something?)
However, because the operator overload is not virtual in std::ostream, covariant return types aren't possible; so std::stringstream << whatever just returns a plain std::ostream&, which then doesn't have a .str() member. To get around this requires a surprising amount of really nasty casting.
Quote:You could use boost::lexical_cast to convert values to strings and use string concatenation
Or you could use boost::format, or roll up something similar yourself (but if you want to DIY then we're getting out of For Beginners territory).
Quote:Original post by Zahlman
std::stringstream::str() returns by value (to mutate the underlying string, you have to call an overload ::str(const std::string&)), so as long as the function accepts a value or a const reference, there should be no problem with the binding. (Unless I've missed something?)
std::ostream & operator<<(std::ostream &, const std::string &) is a non-member operator overload. std::stringstream() creates a temporary rvalue, which cannot be bound to the first argument of the operator overload.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement