Rvalue reference constructor incorrectly called with an lvalue reference
When I compile this code:
class Base { /*...*/ };
class Derived : public Base { /*...*/ };
class C
{
public:
template<typename T>
C(T const& inBase) : baseInC(new T(inBase)) { /*...*/ }
template<typename T>
C(T&& inBase) : baseInC(new T(std::move(inBase))) { /*...*/ }
std::unique_ptr<Base> baseInC;
};
int main()
{
Base base;
Derived derived;
C ca(base);
C cb(derived);
C cc( (Base()) );
C cd( (Derived()) );
return 0;
}
I get a the compiler message:
In instantiation of C::C(T&&) [with T = Base&]': required from C ca(base);
error: new cannot be applied to a reference type
In instantiation of C::C(T&&) [with T = Derived&]': required from C
cb(derived); error: new cannot be applied to a reference type
It looks like C ca(base); is being associated with the rvalue reference
ctor call. Why is the compiler having difficulty associating this line
with the first ctor? The construction of cc and cd works as expected if I
comment out the offending lines.
No comments:
Post a Comment