【发布时间】:2018-07-11 19:04:28
【问题描述】:
我一直喜欢这样的 SFINAE 函数语法,似乎一般都很好用!
template <class Integer, class = typename std::enable_if<std::is_integral<Integer>::value>::type>
T(Integer n) {
// ...
}
但是当我想在同一个班级这样做时遇到了一个问题......
template <class Float, class = typename std::enable_if<std::is_floating_point<Float>::value>::type>
T(Float n) {
// ...
}
遇到这样的错误:
./../T.h:286:2: error: constructor cannot be redeclared
T(Float n) {
^
./../T.h:281:2: note: previous definition is here
T(Integer n) {
^
1 error generated.
这些构造函数不应该只存在于适当的类型中,并且不能同时存在吗?为什么会发生冲突?
我是不是有点厚脸皮了?
另一方面,这确实有效(但我不太喜欢这种语法):
template <class Integer>
T(Integer n, typename std::enable_if<std::is_integral<Integer>::value>::type* = nullptr) {
}
template <class Float>
T(Float n, typename std::enable_if<std::is_floating_point<Float>::value>::type* = nullptr) {
}
【问题讨论】: