Does a universal link template designer hide constructor movement?

Does this template hide ctor ctor?

class A { public: template<typename T> A(T &&t); // move would be as this: /* A(A &&a); */ }; 

And how should I implement move ctor in this situation? Should it be with default syntax A (A &&) or template specialization?

+6
source share
2 answers

According to the standard (draft)

[class.copy]

3 A non-template constructor for class X is a move constructor if its first parameter is of type X & &, const X & &, volatile X & &, or const volatile X & &, and either there are no other parameters, or all other parameters have arguments by default (8.3.6). [Example: Y :: Y (Y & &) is a move constructor.

Only constructors without templates can be moving constructors. The same goes for copy constructors . Therefore, an implicit move constructor is created.

You implement the move constructor in the usual way. Specialization will not work, because an implicit move constructor without a pattern is preferred using overload resolution.

If the type of the argument does not exactly match const T& , however, the template link wins overload resolution. This can easily happen, as can be seen from the example of Praveen.

+3
source

The accepted answer is incorrect. Although it is true that template <typename T> A(T &&t) { } not a move constructor, you already knew that. The compiler will implicitly declare the move constructor in this case, and the normal overload resolution will work as expected:

 A a{2}; // calls template A b = std::move(a); // calls move A c{a}; // calls template 

There is nothing to prevent a moving to c , although the template constructor is not a "move" constructor.

+5
source

All Articles