Type trait to check that all types in a parameter pack are copy constructible

后端 未结 4 495
生来不讨喜
生来不讨喜 2020-12-08 04:36

I need a type trait to check whether all types in a parameter pack are copy constructible. This is what I\'ve done so far. The main function contains some test cases, to che

4条回答
  •  春和景丽
    2020-12-08 05:18

    If the inheritance from std::true_type or std::false_type is not important, then this can be done in straightforward way without SFINAE:

    template 
    struct areCopyConstructible;
    
    template<>
    struct areCopyConstructible<> : std::true_type {};
    
    template 
    struct areCopyConstructible {
        static constexpr bool value = std::is_copy_constructible::value
            && areCopyConstructible::value;
    };
    

    If you want to inherit from std::true_type or std::false_type, you can use std::conditional:

    template 
    struct areCopyConstructible;
    
    template<>
    struct areCopyConstructible<> : std::true_type {};
    
    template 
    struct areCopyConstructible : 
        std::conditional::value,
            areCopyConstructible,
            std::false_type
        >::type
    {};
    

提交回复
热议问题