template<typename T>
class Numeric
{
public:
Numeric() : val(T()) { cout << "ctor default\n"; }
explicit Numeric(const T& v) : val(v) { cout << "ctor value\n"; }
Numeric(const Numeric& v) : val(v.val) { cout << "copy ctor\n"; }
Numeric(Numeric&& v) { val = v.val; cout << "cmove\n"; v.val = 0; }
Numeric& operator=(const Numeric& v) { val = v.val; cout << "copy assignment\n"; return *this; }
Numeric& operator=(Numeric&& v) { val = v.val;cout << "amove\n"; return *this; }
~Numeric() { cout << "dtor\n"; };
private:
T val;
};
// ----------- main ------
Numeric<int> c1(Numeric<int>(2)); // calls the normal constructor instead of copy constructor
I would expect that the copy constructor gets called, but thats not the case, instead the constructor for value initialization gets called.
What is going on here? It seems there is an inplicit conversion going on, but i dont understand why.
if i explicitly convert it, like this
Numeric<int> c1(Numeric<int>(Numeric<int>(2)));
the move constructor and than the destructor are getting called.