Little-known feature of CTAD (class template argument deduction) in C++17: you can mark user-defined deduction guides as explicit
.
(Godbolt.)
template<class T> struct A { A(int); }; A(int) -> A<int>;
template<class T> struct B { B(int); }; explicit B(int) -> B<int>;
A<int> a = 1; // OK, constructor of A<int> is implicit
B<int> b = 1; // OK, constructor of B<int> is implicit
auto a = A(1); // OK, deduction guide of A exists
auto b = B(1); // OK, deduction guide of B exists
A a = 1; // OK, deduction guide of A is non-explicit
B b = 1; // ERROR!! deduction guide of B is explicit
So, class templates A
and B
have observably different behavior.
I'd like to write a unit test that static_assert
s that one of my templates behaves like B
, not like A
.
static_assert(!has_properly_explicit_deduction_guide_v<A>);
static_assert(has_properly_explicit_deduction_guide_v<B>);
Is this possible in C++17 and/or C++20 and/or "C++future"?