Check for arguments type in a variadic template declaration

前端 未结 3 1407
终归单人心
终归单人心 2020-12-19 15:22

I got a plain variadic template declaration, just like the classic one:

template 
class VariadicTemplate;

What

3条回答
  •  感情败类
    2020-12-19 15:46

    Here there is anothe solution :P

    Here it is:

    template  struct static_all_of;
    
    // do recursion if the first argument is true
    template 
    struct static_all_of : static_all_of {};
    
    // end recursion if first argument is false
    template 
    struct static_all_of : std::false_type {};
    
    // end recursion if no more arguments need to be processed
    template <> struct static_all_of<> : std::true_type {};
    
    
    // First template argument is given as the type checking for the is_base_of() function
    
    template 
    class CollectionOfCommonBase : public Requirements...
    {
      static_assert(static_all_of::value...>::value, "One or more template arguments are not base_of the one specified - given template specialization is not allowed.");
    };
    

    So you got it working for:

    class Foo {};
    
    class AFoo : public Foo {};
    class BFoo : public Foo {};
    
    class MyCollection : public CollectionOfCommonBase {};
    

提交回复
热议问题