Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何检查该模板';s参数类型是整数?_C++_Templates - Fatal编程技术网

C++ 如何检查该模板';s参数类型是整数?

C++ 如何检查该模板';s参数类型是整数?,c++,templates,C++,Templates,在一些std模板函数的描述中,我看到如下内容: 如果模板参数为整型,则行为为某某。 否则就是这样那样 我如何做类似的测试?也许是你的演员 既然我写的函数是供我个人使用的,我只能依靠自己提供正确的参数,但为什么要错过学习的机会呢?:) : #包括 #包括 平凡结构 { int-val; }; int main() { std::cout提供了is_积分()如在另一个响应中所描述的,如果编译器还没有支持下一个标准的C++特性, < P>除了其他答案,还应该注意,测试可以在运行时使用,也可以在编

在一些std模板函数的描述中,我看到如下内容:

如果模板参数为整型,则行为为某某。
否则就是这样那样

我如何做类似的测试?也许是你的演员

既然我写的函数是供我个人使用的,我只能依靠自己提供正确的参数,但为什么要错过学习的机会呢?:)

#包括
#包括
平凡结构
{ 
int-val;
}; 
int main()
{ 

std::cout提供了is_积分()如在另一个响应中所描述的,如果编译器还没有支持下一个标准的C++特性,

< P>除了其他答案,还应该注意,测试可以在运行时使用,也可以在编译时根据类型是否完整选择正确的实现:

运行时版本:

// Include either <boost/type_traits/is_integral.hpp> (if using Boost) 
// or <type_traits> (if using c++1x)
// In the following, is_integral shoudl be prefixed by either boost:: or std::

template <typename T>
void algorithm(const T & t)
{
    // some code

    if (is_integral<T>::value)
    {
        // operations to perform if T is an integral type
    }
    else
    {
        // operations to perform if T is not an integral type
    }

    // some other code
}

调用
算法
函数时,编译器将根据模板参数是否为整数来“选择”正确的实现。

如果无法使用C++11功能,则按
std::is_integral::value
执行,并可与C++98一起使用


请注意,98版本是integer,而不是integral

,可能不是,我刚刚检查过我没有包含文件的类型。应该添加“linux”和“gcc”标记…@davka:这些标记与boost无关。如果缺少标头,则需要向sear添加正确的位置ch path for Header或install boost。我想说,在您的第一个示例中,每一个值得使用的编译器都会消除死分支-这很简单。@Georg Fritzsche:这是真的,这就是为什么我没有说任何关于潜在性能增益的内容。不过,我认为重载方法更清楚:这两个实现可以发展实际上,我发现更好的办法是区分静态的(编译时已知/执行的)和动态的(运行时已知/执行的)。但是,这可能是因为我在过去几个月里花了太多时间做元编程!@Georg Fritzsche:另一个重要的观点是,第一种解决方案并不总是一个选项,因为它可能会导致编译器在尝试执行仅适用于某些类型的操作时出错。例如,在integral-specialized alg中我可以使用位运算符;如果我在第一个示例中这样做,那么用于实例化函数的所有类型都需要为这些运算符提供支持(这是我不希望强加给算法用户的!).我知道,我没有批评其他任何东西,我个人更喜欢第二个版本…读“运行时版本”只是误导,因为它很可能在编译时被消除。请注意,is_integral现在是C++11中的标准。
// Include either <boost/type_traits/is_integral.hpp> (if using Boost) 
// or <type_traits> (if using c++1x)
// In the following, is_integral shoudl be prefixed by either boost:: or std::

template <typename T>
void algorithm(const T & t)
{
    // some code

    if (is_integral<T>::value)
    {
        // operations to perform if T is an integral type
    }
    else
    {
        // operations to perform if T is not an integral type
    }

    // some other code
}
#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/is_integral.hpp>

template <typename T>
typename boost::enable_if<boost::is_integral<T> >::type
algorithm(const T & t)
{
    // implementation for integral types
}

template <typename T>
typename boost::disable_if<boost::is_integral<T> >::type
algorithm(const T & t)
{
    // implementation for non integral types
}