Simple variadic template function can't instantinate

后端 未结 2 1725
夕颜
夕颜 2020-12-19 15:42

I\'m aware that sizeof...(Args...) yields the number of types in a C++0x packed template argument list, but I wanted to implement it in terms of other features

2条回答
  •  醉酒成梦
    2020-12-19 16:03

    You didn’t declare a base case. You have a template-free overload of your num_args function but when calling a function num_args() this will never be found, for obvious reasons: it will always try to instantiate a function template.

    You can however specialise your function template to perform the desired operation.

    template <>
    constexpr size_t num_args<>()
    {
        return 0;
    }
    

    However, this won’t work either since here you’re specialising a parameterless function template and such a template doesn’t exist: your other function template num_args always has at least one argument, H.

    In order to really make this work you need partial specialisations, and these only exist for class templates. So this is what you need here.

    template 
    struct num_args_t;
    
    template <>
    struct num_args_t {
        static size_t const value = 0;
    };
    
    template 
    struct num_args_t {
        static size_t const value = num_args_t::value + 1;
    };
    
    template 
    constexpr size_t num_args() {
        return num_args_t::value;
    }
    

提交回复
热议问题