ref-qualifiers for the assignment operator of standard library types

本秂侑毒 提交于 2021-02-09 10:55:43

问题


I was wondering, is there a reason the assignment operator of standard types is not lvalue ref-qualified? None of them are.

Because of that, we can write things such as this:

std::string{} = "42";
std::string s = "hello " + std::string{"world"} = "oops!";

std::vector<int> v = { 1,2,3 };
std::move(v) = { 4,5,6 };

If the assignment operator was lvalue ref-qualified all of these examples would not compile.

Is it because there's a lot of things to modify (but then so it was for noexcept) and nobody wrote a proposal for? I don't think people write code like this but shouldn't the library be designed so that it doesn't even allow it?


回答1:


Your suggestion was proposed in 2009, and ultimately rejected in Frankfurt that year over "concerns about backwards compatibility".

It would have been a breaking change, and we don't like those.

The existing prohibition against assigning to rvalues of built-in types is only of limited real value anyway, so the cost of potentially breaking existing code was almost certainly deemed to be "not worth it".

Would the library be designed in this manner if we had a clean slate? Perhaps.



来源:https://stackoverflow.com/questions/53007802/ref-qualifiers-for-the-assignment-operator-of-standard-library-types

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!