Subject: Re: [boost] [Operators] Update, const-correctness and operator chaining
From: Jonathan Wakely (jwakely.boost_at_[hidden])
Date: 2013-07-01 18:13:46
On 1 July 2013 22:42, Andrew Ho wrote:
> This allows this to be done:
> val1 + val2 = val3;
> changing the return type to const T would allow the compiler to catch this,
> but now
> doing val1 + val2 + val3 must always use copy semantics instead of move
> I can't think of a way which would fix this issue without breaking the use
> of move
You can put an lvalue ref-qualifier on the assignment operator, to
forbid assignment to rvalues.
T& operator=(const T&)&;
I'm not sure it's really beneficial to do so though.
> How should we proceed with this? It's obviously bad usage, but
> compiler should be able to catch this since it's easy to mistype + as =.
> Interestingly enough, doing testing with std::string shows the same
> std::string str1("hello"), str2(" "), str3("world");
> str1 + str2 = str3;
> I tested this issue with std::string and found it in vs2012, gcc 4.3.2, and
> mingw/gcc 4.8.0. I don't have access to a compiler which pre-dates
> C++0x/C++11 so I
> don't know if this issue has always been present. Compiling with gcc 4.8.0
> with -
> std=c++03 still has the issue.
The example is correct C++03. I'm not aware of it being a big
problem, despite being part of the language forever.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk