【发布时间】:2019-04-28 10:51:26
【问题描述】:
问题
假设我们有一个(虚构的)类模板C<T>,它带有一个条件显式的默认构造函数。当且仅当std::is_same_v<T, int> 时,默认构造函数应该是显式的。
A search on "[c++] conditionally explicit" 返回此结果:Constructor conditionally marked explicit。
一个失败的解决方案
接受的答案举了一个例子:
struct S { template <typename T, typename std::enable_if<std::is_integral<T>::value, bool>::type = false > S(T) {} template <typename T, typename std::enable_if<!std::is_integral<T>::value, bool>::type = false> explicit S(T) {} };
稍微修改示例给出了这个使用熟悉的方法std::enable_if的实现:
template <class T>
class C {
public:
template <std::enable_if_t<std::is_same_v<T, int>, int> = 0>
C() {}
template <std::enable_if_t<!std::is_same_v<T, int>, int> = 0>
explicit C() {}
};
不幸的是,这甚至无法编译:demo
prog.cc: In instantiation of 'class C<int>':
prog.cc:15:10: required from here
prog.cc:10:12: error: no type named 'type' in 'struct std::enable_if<false, int>'
10 | explicit C() {}
| ^
prog.cc: In instantiation of 'class C<double>':
prog.cc:18:13: required from here
prog.cc:7:3: error: no type named 'type' in 'struct std::enable_if<false, int>'
7 | C() {}
| ^
问题似乎是由于构造函数的模板参数遗漏,禁用了SFINAE造成的。
问题
- 为什么不能编译?
- 什么是可能的实现方式?
如果可能,我想避免专攻该课程。
【问题讨论】:
-
c++20 中有一个选项可以向显式添加条件
-
@BiagioFesta 谢谢!你的解决方案对我有用。你能写一个答案并解释细节以便我接受吗?
-
SFINAE 仅在依赖于模板参数时才有效。
-
@PasserBy 感谢“SFINAE 仅在它依赖于模板参数时才有效”这句话!我想这就是我正在寻找的解释。
标签: c++ templates template-meta-programming sfinae explicit