A bug Caused by Implicit Construction of std::string

Recently I met a bug when I was using google protobuf to serialize and deserialize messages.I have a code segment like this:

In the code, there is a weird usage of std::string::data() and MyMessage::ParseFromString() API. It’s a mistake due

Be careful with return-value optimization of C++ compilers

Side Effect of Return-value Optimization This “bug” is caused by return-value optimization and affects both GCC and Microsoft C/C++ Compiler. Assume that we have codes like following:

Compile the code with  g++ main.cpp -O0 -g -std=c++11. Run the program and the output result is