Can a variable template be passed as a template template argument?

后端 未结 1 1399
一向
一向 2021-01-01 22:40

The following nonsensical example does not compile, but is there some other way to pass a variable template as a template template argument?



        
相关标签:
1条回答
  • 2021-01-01 23:24

    Short answer: No.

    Long answer: Yes you can using some indirection through a class template:

    template<typename T>
    constexpr auto zero = T{0};
    
    template<typename T>
    struct zero_global {
        static constexpr auto value = zero<T>;
    };
    
    template<typename T, template<typename> class VariableTemplate>
    constexpr auto add_one()
    {
        return VariableTemplate<T>::value + T{1};
    }
    
    int main()
    {
        return add_one<int, zero_global>();
    }
    

    Live example

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