开发者

Calling base class move ctor [C++0x]

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

thi开发者_运维百科s (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).


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).


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜