将 SFINAE 用于模板化构造函数时出现问题
trouble using SFINAE for templated constructor
以下 2 有什么区别:
template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type>
explicit Approx(const T& value) {}
与
template <typename T>
explicit Approx(const typename std::enable_if<std::is_constructible<double, T>::value, T>::type& value) {}
Approx
是普通类(非模板化(的构造函数,我需要double
可以从中构造
我的问题是第一个有效,但不是 C++98(默认模板参数等 - 我有自己的 enable_if
和 is_constructible
的 c++98 特征(
我问的原因是因为我想支持double
的强大类型定义:
class Volatility {
double underlying_;
public:
explicit Volatility(double u) : underlying_(u) {}
explicit operator double() const { return underlying_; }
};
Approx(Volatility(1.)); // error
你的 C++03 版本不起作用,因为它无法推断出 T,给定参数。 构造函数的常用 C++03 机制是一个额外的默认参数。
template<typename T>
explicit Approx(const T& value, typename std::enable_if<std::is_constructible<double, T>::value>::type* dummy = 0) {}
T
以这种形式是可推导的,如果T
满足enable_if
指定的期望,则额外的参数最终void*
。
相关文章:
- C++LinkedList问题.数据类型之间存在冲突?没有匹配的构造函数
- constexpr构造函数需要常量成员函数时出现问题
- 有关 c++ 构造函数的问题
- 关于复制构造函数的一个棘手问题
- 复制构造函数隐式转换问题
- 构造函数 (C++) 中的 char 指针参数存在问题
- C++:关于使用 Stroustrup 示例移动构造函数/赋值的问题
- 关于 std::bitset 构造函数的几个问题?
- 构造函数和 G++ 编译配方的问题
- 在 boost::variant 中类 holden 的复制构造函数存在问题
- C++ 关于两个类构造函数的问题
- 在构造函数 c++ 中初始化属性时出现问题
- 空构造函数和值传递问题
- 我是否需要在虚拟继承类的构造函数中初始化基类以解决菱形继承问题?
- 复制和赋值构造函数的问题
- 变分模板递归构造函数问题
- C++中用户定义的构造函数出现问题
- C ++回测问题:如何检查构造函数在假定失败时是否失败(给定输入的无效参数)
- C++的抽象类继承和构造函数的问题
- 错误 C2280 / 在 std::vector 中实例化对象时复制构造函数出现问题?