可变模板值作为结构的模板参数
variadic template value as template argument for struct
我试着做这样的事情:
template <int v1, template <typename... Args> Args... vx> struct Sum {
const static int RESULT = v1 + Sum<vx...>::RESULT;
};
template <int v> struct Sum {
const static int RESULT = v;
}
像这样使用:
int a = Sum<1, 2>::RESULT;
int b = Sum<1, 2, 3, 4, 5>::RESULT;
很明显,这里出了问题,我正在努力解决可变模板作为结构/类定义中的值的概念。有可能做这样的事吗?怎样
谢谢。。。
其中一个问题是,两个模板声明都没有专门针对另一个和。这些声明中的代码不同,因此代码格式不正确。
此外,您实际上并没有在这里使用模板作为模板参数,代码也不需要它,正如您在这里看到的:
// main template
template <int v1, int... vx> struct Sum {
const static int RESULT = v1 + Sum<vx...>::RESULT;
};
// specialization to make recursion terminate
// the list of matched template parameters is listed
// after the name of the struct in angle brackets
template <int v> struct Sum<v> {
const static int RESULT = v;
};
static_assert(Sum<1, 2, 3, 4, 5>::RESULT == 15, "");
int main() {}
相关文章:
- 使用不带参数的函数访问结构元素
- 将结构字段的类型展开为可变模板参数
- MSVC将仅移动结构参数解释为指针
- 如何使我的 sizeof sum 结构与空参数包一起工作
- 如何传递带有通过引用传递的结构参数的函数?
- 展开可变参数模板结构
- 在类构造函数中定义结构变量的参数
- 如何在方法中传递结构参数
- 我应该在C++中将这些结构用作参数化构造函数吗?
- 在模板方法 c++ 中传递结构参数
- 结构参数在函数调用时损坏
- 如何使用结构参数委托构造函数
- 错误 C2783:无法推断结构参数的模板参数
- 用于存储元组键的数据结构:参数关系列表
- 使用结构参数从程序集中调用C++函数
- 如何为llvm IR调用指令创建结构参数
- c++为函数使用结构参数而不是多个参数
- 导入结构参数
- 将结构参数传递给结构内的函数
- 带有结构参数的pthread不起作用