【发布时间】:2020-02-27 05:39:33
【问题描述】:
已编辑(原始问题只有 int A,int B):
当特化之间比较的#arguments 相同时,模板参数推导按预期工作,但当它们不同时失败(由于包括包含在其中一种特化中的默认参数)。
例如:为什么模板参数推导在一种情况下与另一种情况下失败,有人可以指出任何解释这一点的资源/标准吗?
// 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’的模版实例化不明确
【问题讨论】: