【发布时间】:2021-10-15 22:14:23
【问题描述】:
C++17 CTAD(类模板参数推导)鲜为人知的特性:可以将用户定义的推导指南标记为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
因此,类模板A 和B 具有明显不同的行为。
我想写一个单元测试,static_asserts 我的一个模板的行为类似于B,而不是A。
static_assert(!has_properly_explicit_deduction_guide_v<A>);
static_assert(has_properly_explicit_deduction_guide_v<B>);
这在 C++17 和/或 C++20 和/或“C++future”中是否可行?
【问题讨论】:
-
嗯,我正在探索
new的潜力(为了节省您的时间:它不会起作用)并发现了更多错误:gcc.gnu.org/bugzilla/show_bug.cgi?id=101988 和bugs.llvm.org/show_bug.cgi?id=51547。好玩!
标签: c++ c++17 c++20 template-meta-programming ctad