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

前端 未结 4 1562
[愿得一人]
[愿得一人] 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:43

    I'm in line with Brian Rodriguez's and Piotr Scontnincki's answers, as far as it concerns the fold expressions part. Until folding expressions are in, you could shrink the existing code a little bit by getting rid of the incomplete primary templates as follows:

    template 
    struct is_one_of {
        static constexpr bool value = false;
    };
    
    template 
    struct is_one_of {
        static constexpr bool value =
            std::is_same::value || is_one_of::value;
    };
    
    template 
    struct is_unique {
        static constexpr bool value = true;
    };
    
    template 
    struct is_unique {
        static constexpr bool value = is_unique::value && !is_one_of::value;
    };
    

提交回复
热议问题