Non-type variadic function templates in C++11

前端 未结 5 1449
有刺的猬
有刺的猬 2020-12-05 23:48

I saw a blog post which used non-type variadic templates (currently not supported by gcc, only by clang).

template 
stru         


        
5条回答
  •  既然无缘
    2020-12-06 00:18

    Here are two ways of defining a variadic function template only accepting int parameters. The first one generates a hard-error when instantiated, the second uses SFINAE:

    template
    struct and_: std::true_type {};
    
    template
    struct and_
    : std::integral_constant<
        bool
        , First::value && and_::value
    > {};
    
    template
    void
    foo(T... t)
    {
        static_assert(
            and_...>::value
            , "Invalid parameter was passed" );
        // ...
    }
    
    template<
        typename... T
        , typename = typename std::enable_if<
            and_...>::value
        >::type
    >
    void
    foo(T... t)
    {
        // ...
    }
    

    As you can see, non-type template parameters aren't used here.

提交回复
热议问题