I have a class template like this:
template <typename T>
class MyClass
{
public:
MyClass(const T & val); // First
MyClass(T&& val); // Second
};
Basically I want MyClass to be constructible from a T
whether it's an rvalue or an lvalue. Now when I have something like
const A& foo = ...;
MyClass<const A&> x(foo);
I get redefinition error for MyClass(const A & val)
.
I presume this is because T&& is a universal reference and due to reference collapsing rules, the second constructor also gets converted to having the same signature as the first.
Firstly is my understanding of the error scenario correct? secondly how can I get around this problem(I want to able the use the optimizations that move semantics provide when constructing MyClass)?
Aucun commentaire:
Enregistrer un commentaire