【发布时间】:2018-08-27 18:47:51
【问题描述】:
我有一个由具有静态成员函数的模板模板类参数化的类:
template <template <typename> class F>
struct A {
static int foo();
};
这个类没有foo的默认定义,必须专门针对不同的类型。
我还有另一个由带有嵌套模板类的模板模板类参数化的类:
template <template <typename> class F>
struct B {
template <typename T>
struct C {};
};
我希望 C 专门化 A 用于已经专门化 A 的任何模板模板类 F:
template <template <typename> class F>
struct A<B<F>::template C> {
static int foo();
};
template <template <typename> class F>
int A<B<F>::template C>::foo() {
return A<F>::foo() / 2;
}
所以,如果我有专门的课程A:
template <typename T>
struct E {};
template <>
int A<E>::foo() {
return 42;
}
我希望能够像这样使用专业化(并返回 21):
int bar() {
return A<B<E>::template C>::foo();
}
但是,这无法链接 - 它找不到对 A<B<E>::C>::foo() 的引用。
(请注意,所有这些都在一个文件中 - 这里的标题没有什么奇怪的事情发生)
编译器似乎正在尝试使用A 的主模板而不是特化,这意味着foo 未定义。为什么它在这种情况下不使用专业化?
完整示例
template <template <typename> class F>
struct A {
static int foo();
};
template <template <typename> class F>
struct B {
template <typename T>
struct C {};
};
template <template <typename> class F>
struct A<B<F>::template C> {
static int foo();
};
template <template <typename> class F>
int A<B<F>::template C>::foo() {
return A<F>::foo() / 2;
}
template <typename T>
struct E {};
template <>
int A<E>::foo() {
return 42;
}
int bar() {
// Link fails - error: undefined reference to 'A<B<E>::C>::foo()'
return A<B<E>::template C>::foo();
}
【问题讨论】:
标签: c++ templates partial-specialization template-templates