#include <iostream>
#include <string>
int main()
{
std::string a;
std::string b;
a + b = "dadas";
}
PS D:\WeCode\local_c++> & 'c:\Users\z00841303.vscode\extensions\ms-vscode.cpptools-1.5.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-1vidcjmy.adn' '--stdout=Microsoft-MIEngine-Out-ur4nmbvf.4bj' '--stderr=Microsoft-MIEngine-Error-kdush30f.pk5' '--pid=Microsoft-MIEngine-Pid-5hgspuj4.obh' '--dbgExe=D:\Program Files\mingw\MinGW\bin\gdb.exe' '--interpreter=mi'
a+b is an rvalue expression. Why can a value be assigned in this way?
It seems one of possible reasons is that ref-specifiers for functions were introduced in the C++ Standard after the class template
std::basic_string
was defined in the C++ Standard. Otherwise the assignment operator could be declared for example likeHere is a demonstration program that shows how to suppress an assignment to rvalue.
Anpther reason is to allow chaining operations that defined as functions for user-defined types.