【发布时间】:2012-01-03 21:12:53
【问题描述】:
以下代码:
using namespace std;
template <typename X>
class Goo {};
template <typename X>
class Hoo {};
template <class A, template <typename> class B = Goo >
struct Foo {
B<A> data;
void foo1();
void foo2();
};
template <typename A>
void Foo<A>::foo1() { cout << "foo1 for Goo" << endl;}
int main() {
Foo<int> a;
a.foo1();
}
给我一个编译器错误:
test.cc:18: error: invalid use of incomplete type 'struct Foo<A, Goo>'
test.cc:11: error: declaration of 'struct Foo<A, Goo>'
为什么我不能部分专门化 foo1() ?如果不是这样,我该怎么做?
我还有一个问题:如果我希望只为 A=int, B=Hoo 定义 foo2() 怎么办? 而不是任何其他组合,我该怎么做?
【问题讨论】:
-
这不是部分特化,Foo 有两个参数,你只在
Foo<A>::foo1中指定一个。 -
@BoPersson:我认为我是部分专业化的。第二个模板参数默认是Goo。
-
Goo
中的 X 是什么。 Goo 是带有参数 X 的模板。它在哪里指定?
标签: c++ templates template-specialization