Variadic templates with exactly n parameters

前端 未结 1 1843
梦如初夏
梦如初夏 2020-12-20 08:24

I want to make a variadic template with exactly N arguments, where N is also a template parameter. For example,

template 
void fu         


        
相关标签:
1条回答
  • 2020-12-20 08:50

    You can use std::enable_if instead of static_assert:

    template <std::size_t N, typename ...Args>
    auto function(Args&&... args)
        -> typename std::enable_if<N == sizeof...(Args), void>::type
    {
        ...
    }
    

    Update: It's also possible to use it in constructors, where N is a template argument of the class.

    template <std::size_t N>
    struct foobar
    {
        template <typename ...Args, typename = typename std::enable_if<N == sizeof...(Args), void>::type>
        foobar(Args&&... args) { ... }
    };
    
    0 讨论(0)
提交回复
热议问题