a reference of type "std::string&" (not const-qualified) cannot be initialized

There are two problems with your code. First, "Hello World!" is a char const[13], not an std::string. So the compiler has to (implicitly) convert it to an std::string. The result of a conversion is a temporary (rvalue in C++-speak), and you cannot initialize a reference to a non-const with a temporary. The second is that even if you could (or you declared the function to return a reference to const), you're returning a reference to something which will immediately go out of scope (and thus be destructed); any use of the resulting reference will result in undefined behavior.

The real question is: why the reference? Unless you're actually referring to something in an object with a longer lifetime, with the intent that the client code modify it (usually not a good idea, but there are notable exceptions, like operator[] of a vector), you should return by value.


"Hello World" isn't a string, it is an array of char. The c++ compiler needs to convert this into a string value, not a string reference (because it's not a string), so your function should look like:

string foo(){
    return "Hello World";
}

To expand (at the OP's request) the compiler does something like this:

string foo(){
    char a[] = "Hello World";
    string s( a ); 
    return s;
}

The value s is copied out of the function by the std::string copy constructor.


You're instructing the compiler to return a temporary std::string created from the char array "Hello World". It needs to put this somewhere and have somebody responsible for cleaning it up. You can do that in a few ways:

  • Return an auto_ptr
  • Return a string object
  • Return a const reference to a string object (although this does leave me with the question who cleans it up?)
  • (c++0x only) Return a right-hand-reference to an std::string. (std::string &&)

The second would probably be the easiest. The compiler will use RVO (return value optimization) to remove the copy invoked by that.

Tags:

C++

String