使用默认参数的模板参数推导行为
Template argument deduction behavior with default arguments
EDITED(原始问题只有int A,int B(:
当两个专门化之间比较的#参数相同时,模板参数推导会按预期工作,但当它们不同时(由于其中一个专门化中包含默认参数(,模板参数演绎会失败。
例如:为什么模板参数推导在一种情况下与另一种情况相比失败了,有人能指出任何解释这一点的资源/标准吗?
// Example program
#include <iostream>
template <int A, int B, int C, int D=1>
class Foo;
template <int A, int B, int C>
class Foo <A, B, C>
{
public:
int a;
Foo()
{
a = 0;
}
};
template <int D> // Fails compilation
class Foo <1, 1, 1, D> // Fails compilation
//template <> // works, prints a = 1
//class Foo <1, 1, 1> // works, prints a = 1
{
public:
int a;
Foo()
{
a = 1;
}
};
int main()
{
Foo <1, 1, 1, 1> f;
std::cout << "a = "<< f.a << std::endl;
}
错误:"class Foo<1,2,1,1>'
template <int A>
class Foo <A> {/*..*/};
具有默认参数:
template <int A>
class Foo <A, 1> {/*..*/};
带(有效(:
template <int B>
class Foo <1, B> { /*..*/ };
您对Foo<1, 1>
有歧义,Foo<A, 1>
和Foo<1, B>
都匹配,但都不太专业。
template <> class Foo <1> { /**/};
是
template <> class Foo <1, 1> { /**/};
并且比之前的两个专业都更专业。
相关文章:
- 使函数参数默认为周围范围
- 如何将值传递给其参数(默认参数)
- 当“std::make_index_sequence”和“std::index_sequence”用于模板参数默认类型时
- C ,构造器中允许的参考参数默认值
- 哪些规则控制参数默认赋值?
- 错误:上一个规范后给出的参数默认参数
- 是否可以设计一个包含模板参数默认值的类
- 构造函数中参数C++默认值
- 如何设置依赖于其他参数的参数默认值
- C++模板参数默认函数实现
- QInput对话框参数默认值
- g++4.8.2上列表方法参数默认初始化时出错
- 在函数定义中指定参数默认值会导致错误 C2143:语法错误:'='之前缺少')'
- 函数模板:将第一个模板参数默认为第二个
- 模板模板参数默认可以引用其他模板类型的参数
- 参数默认为先例参数的值
- 如何修改一个类,使它只有一个成员函数,所有参数默认
- 内置类型的模板参数默认值
- 是否可以使用构造函数或对象作为其他类方法的参数默认值?
- 模板参数默认为更高版本