Saturday, 15 March 2014

c++ - GCC 4.8 with GNU STL produces bad code for std::string constructor? -



c++ - GCC 4.8 with GNU STL produces bad code for std::string constructor? -

so bit of c++ code:

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

which compiles fine gcc 4.8 , vs 2013. c++ knowledge, code okay local variable thestring beingness brought scope hides thestring function argument. @ point of thestring construction, thestring in scope function argument passed std::string constructor. constructed std::string named thestring comes scope , thestring used later in code. phew!

however, gcc seems deed thestring passed std::string constructor local thestring (which hasn't been constructed yet) causing compiled programme crash. vs 2013 code compiles , runs fine.

so,

is code correct? or doing outside spec means gcc behaviour undefined. is bug in gcc?

no, code invalid.

according c++ standard (3.3.2 point of declaration)

1 point of declaration name after finish declarator (clause 8) , before initializer (if any), except noted below.

[ example: int x = 12; { int x = x; }

here sec x initialized own (indeterminate) value. —end illustration ]

and (3.3.3 block scope, #2)

a parameter name shall not redeclared in outermost block of function definition nor in outermost block of handler associated function-try-block.

c++ gcc stl undefined-behavior

No comments:

Post a Comment