Kie*_*iel 9 c++ templates sfinae c++11
我想为不同种类的数字(整数,浮点)创建验证器,例如:
typename number_validator<T>::type validator;
Run Code Online (Sandbox Code Playgroud)
我在stdie is_integral和is_floating_point.中发现了有用的特征.我如何使用这些特征来专门化模板number_validator(它是a struct)?
编辑:我正在寻找这样的东西:
template<typename T, typename Enabled>
struct number_validator {};
template<typename T>
struct number_validator<T, typename enable_if<is_floating_point<T>::value, T>::type>
//this doesn't work..
{
typedef floating_point_validator type;
};
Run Code Online (Sandbox Code Playgroud)
eme*_*esx 12
这可能是您正在寻找的,即标签调度:
template<typename T, bool = is_integral<T>::value>
struct number_validator {};
template<typename T>
struct number_validator<T, true>
{
typedef integral_validator type;
};
template<typename T>
struct number_validator<T, false>
{
typedef floating_point_validator type;
};
Run Code Online (Sandbox Code Playgroud)
这假设您确实对数字进行操作,因此类型始终是整数或浮点.