Is there a way to get a given number of inputs where the number is given by a template in compile time in c++?

前端 未结 1 1141
死守一世寂寞
死守一世寂寞 2021-01-17 18:22

For example, suppose I make a class like below:

template 
class A{
public:
    int operator()(int input, ...){ // get INPUT_SI         


        
1条回答
  •  执念已碎
    2021-01-17 18:43

    Live demo 1

    template  using always_t = T;
    
    template 
    struct A_impl;
    
    template 
    struct A_impl>
    {
        int operator ()(always_t...)
        {
            return 0;
        }
    };
    template 
    struct A : A_impl>
    { };
    
    
    A<2>{}(1, 2); // fine
    A<2>{}(1, 2, 3); // fail
    

    and this is a version that allows you to compute the sum of the parameters:

    Live demo 2

    template  using always_t = T;
    
    template 
    struct A_impl;
    
    template 
    struct A_impl>
    {
        constexpr int operator ()(std::tuple...>&& t) {
            auto adder = [](auto... ts) {
                return (0 + ... + ts);
            };
            return std::apply(adder, std::move(t));
        }
    };
    
    template 
    struct A : A_impl>{
    };
    
    constexpr int sum = A<3>{}({1, 4, 5});
    static_assert(sum == 10);
    

    The trick is to use a parameter pack with length N so that we can use it to expand as N times a specific type into the parameter list of A_impl::operator().

    A parameter pack can expand into N repetition of the pattern that (usually) precede ...


    Consider a function like:

    template
    void foo(T...);
    

    T... indicate in simple terms that it can be replaced by successive types into the parameter list of foo, one possible expansion could be foo(int, int, double, char), also notice that what preside ... is an identifier that comes from class... T.


    Returning to the code, we need to generate a parameter pack, we did that through std::make_index_sequence, that generate the sequence 0..(N-1) which is captured by std::size_t... Is, then we use this pack to expand the pattern always_t that is just an alias to T=int, this end up repeating T=int as many times as elements Is contains.

    Note: ellipsis parameter ... is not the same as parameter pack.

    0 讨论(0)
提交回复
热议问题