How to make a variadic is_same?

后端 未结 6 788
刺人心
刺人心 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:02

    Nice and concise with C++17:

    template 
    struct is_any : std::disjunction...> {};
    

    And the dual:

    template 
    struct are_same : std::conjunction...> {};
    

    A variation that uses fold expressions:

    template 
    struct is_any : std::bool_constant<(std::is_same_v || ...)> {};
    
    template 
    struct are_same : std::bool_constant<(std::is_same_v && ...)> {};
    

提交回复
热议问题