Question

Which is the right way to call base class move ctor?

this (works in MSVC2010, but not in CBuilder2010):

struct Foo
{
    Foo(Foo&& other) { }
};

struct Bar : public Foo
{
    Bar(Bar&& other) : Foo((Foo&&)other) { }
};

or (works in CBuilder2010, but not in MSVC2010):

struct Foo
{
    Foo(Foo&& other) { }
};

struct Bar : public Foo
{
    Bar(Bar&& other) : Foo(&other) { }
};

or, are they both wrong? If so, what's the right way (in terms of what's specified in the C++0x standards)?

Note: I can't figure out how to get it to work in CBuilderXE (both versions don't work).

Was it helpful?

Solution

The first option looks logical although I would spell it std::forward<Bar>(other).

I have no idea what causes CBuilder to think that the type of &other is Bar&& and not Bar*.

Basically what's happening here is that once an rvalue reference is named it is no longer an rvalue reference, that's what std::forward is for, it maintains the rvaluness (to coin a phrase), so when you call : Foo(other) you're calling the copy constructor rather than the move constructor.

This should be the idiomatic (and standard compliant) way to achieve what you're trying to do (unless I'm badly misunderstanding the FCD).

OTHER TIPS

The first option is the correct one in terms of the Standard- except that you shouldn't have to cast it and you should forward it. std::forward<Bar>(other) is the correct way- else, you are invoking the copy constructor when you want to invoke the move constructor.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top