warning: returning reference to temporary

This is an example when an unwanted implicit conversion takes place. "" is not a std::string, so the compiler tries to find a way to turn it into one. And by using the string( const char* str ) constructor it succeeds in that attempt.
Now a temporary instance of std::string has been created that will be deleted at the end of the method call. Thus it’s obviously not a good idea to reference an instance that won’t exist anymore after the method call.

I’d suggest you either change the return type to const string or store the "" in a member or static variable of SomeClass.

Leave a Comment