C++ 可变模板值作为结构的模板参数

C++ 可变模板值作为结构的模板参数,c++,templates,variadic,C++,Templates,Variadic,我尝试这样做: 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; } 模板结构和{ const static int

我尝试这样做:

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;
}
模板结构和{
const static int RESULT=v1+Sum::RESULT;
};
模板结构和{
常量静态int结果=v;
}
要这样使用:

int a = Sum<1, 2>::RESULT;
int b = Sum<1, 2, 3, 4, 5>::RESULT;
inta=Sum::RESULT;
int b=总和::结果;
显然,这里有点不对劲,我正在努力解决可变模板作为结构/类定义中的值的概念。有可能这样做吗?怎么做


谢谢…

其中一个问题是,两个模板声明都不专门用于另一个模板声明,而且这些声明中的代码不同,因此代码的格式不正确

此外,此处实际上没有将模板用作模板参数,代码也不需要它,如您所见:

// 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() {}
//主模板
模板结构和{
const static int RESULT=v1+Sum::RESULT;
};
//使递归终止的专门化
//将列出匹配的模板参数列表
//在尖括号中的结构名称之后
模板结构和{
常量静态int结果=v;
};
静态_断言(Sum::RESULT==15,“”);
int main(){}

milleniumbug,非常感谢!我尝试了类似的方法,但没有成功(我想我忘记了Sum的特殊版本中的值)。这正是我想要的。再次感谢@jcdemers不客气。作为一种表达感激的方式,如果你的问题有助于你解决问题,你可以给出答案。如果你的问题已经得到回答,请将解决问题的答案标记为已接受。