我需要为某些类型专门化模板成员函数(比如说double).它工作正常,而类X本身不是模板类,但是当我创建模板时,GCC开始给出编译时错误.
#include <iostream>
#include <cmath>
template <class C> class X
{
public:
template <class T> void get_as();
};
template <class C>
void X<C>::get_as<double>()
{
}
int main()
{
X<int> x;
x.get_as();
}
Run Code Online (Sandbox Code Playgroud)
这是错误消息
source.cpp:11:27: error: template-id
'get_as<double>' in declaration of primary template
source.cpp:11:6: error: prototype for
'void X<C>::get_as()' does not match any in class 'X<C>'
source.cpp:7:35: error: candidate is:
template<class C> template<class T> void X::get_as()
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?这里有什么问题?
提前致谢.
template<int x> struct A {
template<int y> struct B {};.
template<int y, int unused> struct C {};
};
template<int x> template<>
struct A<x>::B<x> {}; // error: enclosing class templates are not explicitly specialized
template<int x> template<int unused>
struct A<x>::C<x, unused> {}; // ok
Run Code Online (Sandbox Code Playgroud)
那么,如果外部类也不是专用的,为什么不允许内部嵌套类(或函数)的显式特化?奇怪的是,如果我只是简单地添加一个虚拟模板参数来部分地专门化内部类,我可以解决这个问题.使事情更丑陋,更复杂,但它确实有效.
我会将完全特化视为部分特化的子集 - 特别是因为您可以通过添加伪参数将每个完整的特化表示为部分.因此,部分和完全专业化之间的消歧对我来说并没有多大意义.
不幸的是,没有人在comp.std.c ++敢于回答,所以我再次以赏金把它放在这里.
注意:我需要此功能用于一组外部类的内部类的递归模板,而内部参数的特化确实取决于外部模板参数.