模板专业化的情况下,如果有变态模板功能
Template specialization in case of variadic template function
i具有此功能,它是一个变异模板函数:
template<uint C>
double foo(){
double cpt = 1;
for(uint i=0; i<10; i++){
cpt += i*C;
}
return cpt;
}
template<uint C1, uint C2, uint... CCs>
double foo(){
double cpt = 1;
for(uint i=0; i<10; i++){
cpt += i*C1;
}
return cpt + foo<C2, CCs...>();
}
它可以按预期完美地工作,但我认为这不是做我想做的事情的正确方法。我试图写这样的东西:
double foo(){
return 0;
}
template<uint C1, uint... CCs>
double foo(){
double cpt = 1;
for(uint i=0; i<10; i++){
cpt += i*C1;
}
return cpt + foo<CCs...>();
}
但是我有错误 no matching function for call foo() note: couldn't deduce template parameter C1
。我还尝试了第一个foo
函数的template <typename T>
,但我的错误也相同。
有人知道为什么吗?我使用的是g 5.4与-std = c 11和-o3旗。
最终迭代将调用foo<>()
。它不匹配double foo() { … }
,因为它不是模板功能。
您无法使用
修复它template <typename T>
double foo() {
return 0;
}
因为无法推导T
。
但是您 can 为T
提供默认值,以便foo<>()
变得有效:
template <typename T = void> // <---
double foo() {
return 0;
}
相关文章:
- 类模板的成员功能的定义在单独的TU中完全专业化
- 特定好友功能专业化
- 如何部分专业化功能以用元组元素作为参数调用功能
- 功能模板参数包,然后是模板参数和专业化
- 不允许功能模板的部分专业化背后的理由是什么?
- 在此功能[ - wuninitialization]中使用非专业化
- 模板专业化的情况下,如果有变态模板功能
- 如何在另一个名称空间内从另一个命名空间内明确专业化功能模板
- 为什么VS2017拒绝我的功能模板专业化,而不是VS2015拒绝
- 仅适用于单个功能的部分模板专业化
- 功能模板专业化具有模板打字名称
- 对完整模板专业类成员功能的未定义引用,但不是部分专业化
- 特定模板类型的功能模板专业化
- 功能模板专业化中的bool返回值
- C 将lambda函数用作模板功能专业化
- 如何正确设计和实施功能模板专业化
- Clang无法在模板类专业化中编译模板功能,该模板类专业化与模板声明具有 *不同的返回类型 *
- 统一功能使用模板专业化和接口来调用实例和原始类型
- 忽略了部分功能专业化中价值类型的最高级别const预选赛
- 功能专业化