Why does this std :: string C ++ code not give a compile-time error?

I have the following snippet:

#include <string>

int main(int argc, char *argv[])
{
    std::string a, b, c;
    a + b = c;
    return 0;
}

Why doesn't this C ++ code give a compile-time error? This is probably due to the way the method was implemented std::string::operator+, but then my question is: why was it implemented in this way? When is this behavior necessary?

+4
source share
1 answer

You can assign temporary objects. There is no rule to prevent this.

If you do not want a member function to be called temporarily (by r value, more generally), you can use ref-qualifier in the function declaration.

, std::string::operator= .

, ; , , , .

+8

Source: https://habr.com/ru/post/1618004/


All Articles