Sign in to follow this  

Is this possible Function( (string1 + string2 + string3 + int) )

This topic is 3101 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

What I'm trying to do is combine things as a string at the point of passing them into a function. example

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, 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?

Share this post


Link to post
Share on other sites
If I understand what you're asking:

std::stringstream sstr;
sstr << string1 << day << " " << month << " , " << year;
MyFunction(sstr.str());

Share this post


Link to post
Share on other sites
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() );

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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) );

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
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++.


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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 3101 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this