How to check that template's parameter type is integral?

前端 未结 4 654
南笙
南笙 2021-01-12 09:11

In the description of some std template function I saw something like:

if the template parameter is of integral type, the behavior is such and such.

4条回答
  •  死守一世寂寞
    2021-01-12 09:54

    One possibility:

    #include  
    #include  
    
    struct trivial 
    { 
        int val; 
    }; 
    
    int main() 
    { 
        std::cout << "is_integral == " << std::boolalpha 
            << std::is_integral::value << std::endl; 
        std::cout << "is_integral == " << std::boolalpha 
            << std::is_integral::value << std::endl; 
        std::cout << "is_integral == " << std::boolalpha 
            << std::is_integral::value << std::endl; 
    
        return (0); 
    } 
    

    So you then use std::is_integral<> to determine the action.

提交回复
热议问题