简体   繁体   中英

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

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

template<typename T>
constexpr auto zero = T{0};

template<typename T, template<typename> auto VariableTemplate>
constexpr auto add_one()
{
    return VariableTemplate<T> + T{1};
}

int main()
{
    return add_one<int, zero>();
}

Try on Compiler Explorer

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM