编程语言
首页 > 编程语言> > c++11新特性std::is_trivial

c++11新特性std::is_trivial

作者:互联网

首先 std::is_trivila 定义:

template< class T >
struct is_trivial;

结构成员函数: value
返回true,如果T 包含默认的构造函数。
其他情况下,返回false。
一种可能的实现方式:

template< class T >
struct is_trivial : std::integral_constant< 
    bool,
    std::is_trivially_copyable<T>::value &&
    std::is_trivially_default_constructible<T>::value 
> {};

样例:

struct A {
    A()= default;
    int m;
};

struct B {
    int m;
};

struct C{
    C(){}
    int m;
};

int main(int argc, char**argv){

    std::cout << std::boolalpha;
    std::cout << std::is_trivial<A>::value << '\n';
    std::cout << std::is_trivial<B>::value << '\n';
    std::cout << std::is_trivial<C>::value << '\n';
    }
    

输出:
true
true
false

标签:11,std,struct,int,true,value,trivial
来源: https://blog.csdn.net/TH_NUM/article/details/95384976