变量模板是否可以作为模板模板参数传递
Can a variable template be passed 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>();
}
试用编译器资源管理器
简短回答:不。
长话短说:是的,你可以通过类模板使用一些间接方法:
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>();
}
实例
相关文章:
- 是否有C++编译器选项允许激进地删除所有函数调用,并将参数传递给具有空体的函数
- 是否可以在不填充自己的参数的情况下将模板函数作为参数传递?
- 是否可以在命令行中将输入参数传递给可执行文件
- 在C++17中,lambda是否可以作为模板参数传递
- 使用 std::move 将参数传递给函数,如果该参数声明为按值传递或使用移动操作数 &&,是否有区别?
- 变量模板是否可以作为模板模板参数传递
- 是否可以将结构的成员变量作为参数传递
- 是否可以交换在 c++ 中作为函数 func(say) 中的参数传递的 const 向量的两个元素,例如:vector<int>func (const vector<int>&
- 是否可以在不使用数组的情况下将不同数量的相同类型的参数传递给函数?
- 是否可以创建一个指向对象的智能指针并将此智能指针作为参数传递给构造函数?
- 是否可以将变量/函数返回作为模板参数传递
- 继承是否是将一组模型参数传递给不同类的可行解决方案
- 是否可以使用将不同参数传递给构造函数的类的不同实例初始化向量
- 是否可以将char **作为参数传递
- 是否可以将 std::d eque 的成员函数作为参数传递?
- 是否可以将可变长度数组作为参数传递C++?
- 将模板原型作为模板参数传递 - 是否可能
- 将正在移动的对象的成员作为参数传递是否安全
- 将指向函数的指针数组作为"int"参数传递是否可以将地址转换为整数?
- 输入参数传递:是否存在有效的按值传递的大小阈值