returning copy of the temporary string object

Q

qazmlp

Is this code reliable? Basically, I want to know whether returning
copy of the temporary string object is valid or not.

#include <iostream>

std::string returnString()
{
return std::string( "TempString" ) ;
}

int main()
{
std::cout<< returnString() << std::endl;
}
 
J

Jeff Schwab

qazmlp said:
Is this code reliable? Basically, I want to know whether returning
copy of the temporary string object is valid or not.

#include <iostream>

std::string returnString()
{
return std::string( "TempString" ) ;
}

int main()
{
std::cout<< returnString() << std::endl;
}

Yep, that's fine.
 
J

John Harrison

qazmlp said:
Is this code reliable? Basically, I want to know whether returning
copy of the temporary string object is valid or not.

#include <iostream>

std::string returnString()
{
return std::string( "TempString" ) ;
}

int main()
{
std::cout<< returnString() << std::endl;
}

Yes completely reliable.

john
 
C

Claudio Puviani

qazmlp said:
Is this code reliable? Basically, I want to know whether returning
copy of the temporary string object is valid or not.

#include <iostream>

std::string returnString()
{
return std::string( "TempString" ) ;
}

int main()
{
std::cout<< returnString() << std::endl;
}

It's reliable in Standard C++. I've seen Microsoft Visual C++ crash while trying
to do that. A fair (and rhetorical question) would be, "Is Visual C++ reliable?"
:)

Claudio Puviani
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top