3

If I have a class like this

template <typename T>
class MyClass
{
    T myData;

public:
    T getValue() { return myData; }        

    template <typename V>
    MyClass(const MyClass<V>& other) :
        myData((T) other.getValue())
    {
    }
};

This would mean that I provide a copy constructor (for V=T) and thus according to this link Why no default move-assignment/move-constructor? I do not get default move constructors etc.

Is there a way to have the templated constructor only work as conversion constructors, so for V!=T?

Community
  • 1
  • 1
NOhs
  • 2,780
  • 3
  • 25
  • 59

1 Answers1

5

Your premise is wrong. A constructor template is never used to instantiate a copy (or move) constructor. In other words, a copy/move constructor is always a non-template member function, even if a member function template could produce a constructor with the appropriate copy/move signature.

So as is, your class will still have a normal copy constructor in addition to the template.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455