小编Kie*_*iel的帖子

整数/实数类型的模板结构专门化

我想为不同种类的数字(整数,浮点)创建验证器,例如:

typename number_validator<T>::type validator;
Run Code Online (Sandbox Code Playgroud)

我在stdie is_integralis_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)

c++ templates sfinae c++11

9
推荐指数
1
解决办法
2760
查看次数

从true值到std :: true_type的隐式转换

这是我编写的用于学习C++的简单模板编程:

#include <type_traits>
#include <iostream>

using namespace std;

template<typename T>
T foo(T t, true_type)
{
    cout << t << " is integral! ";
    return 2 * t;
}


template<typename T>
T foo(T t, false_type)
{
    cout << t << " ain't integral! ";
    return -1 * (int)t;
}

template<typename T>
T do_foo(T t){
    return foo(t, is_integral<T>());
}

int main()
{
    cout << do_foo<int>(3) << endl;
    cout << do_foo<float>(2.5) << endl;
}
Run Code Online (Sandbox Code Playgroud)

它没有任何想象力,但它确实编译和工作.

我想知道该部分是如何is_integral<T>()工作的?

我正在读这个:http://en.cppreference.com/w/cpp/types/is_integral我找不到这种行为的具体描述 …

c++ templates type-traits c++11

3
推荐指数
1
解决办法
839
查看次数

标签 统计

c++ ×2

c++11 ×2

templates ×2

sfinae ×1

type-traits ×1