Probably my understanding of explicit
is insufficient, but I wonder why in the following code the copy constructor is not hidden by the unversal reference constructor when I declare the latter as explicit
.
struct A
{
A() = default;
template<typename T>
A(T&& t) { std::cout<<"hides copy constructor"<<std::endl; }
};
struct A_explicit
{
A_explicit() = default;
template<typename T>
explicit A_explicit(T&& t) { std::cout<<"does not hide copy constructor?"<<std::endl; }
};
int main()
{
A a;
auto b = a; (void) b; //prints "hides copy constructor"
A_explicit a_exp;
auto b_exp = a_exp; (void) b_exp; //prints nothing
}
Is that a general solution instead of the SFINAE stuff one would apply otherwise to prevent the hiding in A
(for example by std::enable_if_t<!std::is_same<std::decay_t<T>, A>::value>
, see here)?
A constructor marked as explicit
does not participate in overload resolution during copy-initialization (A a = b;
, among other things).
It does participate in copy-list-initialization (A a = {b1};
), and causes the program to be ill-formed if selected.
... except when the thing inside the braces is an A
or a class derived therefrom, in which case a recent defect report changed the rules to say that in this particular situation copy-initialization is performed instead - and so explicit
constructors are once again just ignored entirely (demo).
Very teachable, I know.
Is that a general solution instead of the SFINAE stuff one would apply otherwise to prevent the hiding in A?
No. Because that constructor will still win overload resolution for direct-initialization:
A_explicit a, b(a); // will call the constructor taking a forwarding reference