How to make a variadic is_same?

后端 未结 6 781
刺人心
刺人心 2020-12-05 02:07

How can I make a class template that returns whether any of its variadic types are equal to the first type. I want to be able to do this:

is_same

        
6条回答
  •  自闭症患者
    2020-12-05 03:01

    Using the relaxed C++14 constexpr functions, these kinds of things are much easier to code, and probably much faster to compile as well, so you could write:

    template 
    constexpr bool is_all_same() {
        bool pairs[] = {std::is_same::value...};
        for(bool p: pairs) if(!p) return false;
        return true;
    }
    
    template 
    constexpr bool is_any_same() {
        bool pairs[] = {std::is_same::value...};
        for(bool p: pairs) if(p) return true;
        return false;
    }
    

    This is enabled by the fact that in C++14 constexpr functions can have for loops.

提交回复
热议问题