变量模板是否可以作为模板模板参数传递

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

本文关键字:参数传递 是否 变量      更新时间:2023-10-16

下面这个毫无意义的例子没有编译,但是否有其他方法可以将变量模板作为模板模板参数传递?

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>();
}

试用编译器资源管理器

简短回答:不。

长话短说:是的,你可以通过类模板使用一些间接方法:

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>();
}

实例

相关文章: