可以将变量模板作为模板模板参数传递吗? [英] Can a variable template be passed as a template template argument?

查看:105
本文介绍了可以将变量模板作为模板模板参数传递吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下荒谬的示例无法编译,但是还有其他方法可以将变量模板作为模板模板参数传递吗?

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

尝试使用编译器资源管理器

推荐答案

简短答案:否.

长答案:是的,您可以通过类模板使用一些间接方式:

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

实时示例

这篇关于可以将变量模板作为模板模板参数传递吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆