13

So a bit of C++ code:

void func( const std::string& theString )
{
    std::string theString( theString );
    theString += " more string";
    std::cout << theString;
}

which compiles fine with GCC 4.8 and VS 2013. From my C++ knowledge, the code is okay with a local variable theString being brought into scope which then hides theString from the function argument. At the point of theString construction, the only theString in scope is the function argument which is passed to the std::string constructor. The constructed std::string is then named theString which comes into scope and is theString used later in the code. Phew!

However, GCC seems to act like theString passed to the std::string constructor is the local theString (which hasn’t been constructed yet) causing the compiled program to crash. With VS 2013 the code compiles and runs fine.

So,

  1. Is my code correct? Or am I doing something outside spec which means the GCC behaviour is undefined.
  2. Is this a bug in GCC?