Re: C++0x: unfortunate interaction of rvalue references and concepts
On 8 Dez., 04:12, "Joe Smith" <unknown_kev_...@hotmail.com> wrote:
"SG" wrote:
[...] rvalue references are allowed to bind to lvalues. [...]
Really? I was under the impression that a T or a T& (with T not a reference
type) will never bind to a 'T&&'. As I understand it a non-template function
with an argument of type T&& should only be called on temporaries or if a T
has been converted to an rvalue by way of std::move (or some other
mechanism).
Yes. The way it is currently defined is that rvalue references also
bind to lvalues UNLESS there's a more attractive overload. Move
semantics relies on overload resolution to pick the right function.
The motivation behind it was probably to reduce the need for
overloading when:
(*) you don't intent to copy/move objects
(*) but still want to be able to refer/alter to temporary objects.
Example:
class A {
int data;
public:
A() : data(0) {}
void setdata(int x) { data = x; }
void backup(ostream && os) const { os << data; }
};
int main() {
A foo;
foo.setdata(42);
foo.backup(ofstream("myfile.dat"));
}
This is clearly a trade-off. If you made && only bind to rvalues you
could safe some function declarations a la
void push_back(T const&) = delete; // T movable but not copyable.
but would require additional overloads for examples like 'A::backup'
to be able to accept both, lvalue and rvalue arguments.
Cheers!
SG
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]