How can I make this variadic template code shorter using features from C++14 and C++1z?

前端 未结 4 1683
[愿得一人]
[愿得一人] 2020-12-11 02:55

This is a code snippet that I am going to use in order to check whether the variadic template types are unique:

template 
struct is_one_of         


        
4条回答
  •  感情败类
    2020-12-11 03:31

    I'd (now) suggest using the std::conj/disj/nega family of STL functions:

    #include 
    
    template 
    struct is_one_of : std::disjunction...> {};
    
    template 
    struct is_unique : std::conjunction>..., is_unique> {};
    
    template 
    struct is_unique : std::true_type {};
    
    int main()
    {
        static_assert(is_one_of::value);
        static_assert(is_unique::value);
        static_assert(!is_unique::value);
    }
    

    When fold-expressions, which were designed for these cases, are released into the language this will become trivial:

    namespace stx = std::experimental;
    
    template 
    struct is_one_of {
        static constexpr bool value = (stx::is_same_v || ...);
    };
    
    template 
    struct is_unique {
        static constexpr bool value = (!stx::is_same_v && ... && is_unique::value);
    };
    
    template 
    struct is_unique : std::true_type {};
    

提交回复
热议问题