我有一个带模板成员函数的模板类。我想明确地实例化这个类,以避免剧烈的编译放缓。我正在使用g ++ 4.1.2。我从编译器中得到不明确的模板特化错误。这将重现该问题在最短的代码:使用成员模板函数显式模板实例化
template <class T, class S >
class Test
{
public:
template< typename T1 >
void* get(const T1&);
void* get(const int&); //Specialization of the above
};
typedef Test<int, double> foo;
//instantiate
inline template class Test<int, double>;
template void* foo::get(int const&);
我不想用一个包罗万象:
template class Test<int, double>
因为超载GET(const int的&)将不被定义所有可能的显式实例化,因此编译器会抛出不支持它的类型。
这段代码在visual studio中编译(没有内联前面的模板,这是一个gcc特定的扩展)。有人可以告诉我如何让这段代码片段编译?
UPDATE: 这是我的错误:
g++ -c -o template.o template.cpp
template.cpp:14: error: ambiguous template specialization ‘get<>’ for ‘void* Test<int, double>::get(const int&)’
template.cpp:7: error: candidates are: void* Test<T, S>::get(const int&) [with T = int, S = double]
template.cpp:6: error: template<class T1> void* Test::get(const T1&) [with T1 = T1, T = int, S = double]
UPDATE2: 感谢您的解决方案,它不虽然编译。专业课不允许在课堂上进行。错误是:
g++ -c -o template.o template.cpp
template.cpp:7: error: explicit specialization in non-namespace scope ‘class Test<T, S>’
template.cpp:7: error: enclosing class templates are not explicitly specialized
template.cpp:8: error: ‘get’ is not a template function
template.cpp: In instantiation of ‘void* Test<T, S>::get(const T1&) [with T1 = int, T = int, S = double]’:
template.cpp:15: instantiated from here
template.cpp:15: error: explicit instantiation of ‘void* Test<T, S>::get(const T1&) [with T1 = int, T = int, S = double]’ but no definition available
make: *** [template.o] Error 1
'foo'是为了'Test'吗?你能发布实际的错误和它所指的行吗? – 2010-08-28 07:14:09
我键入foo以方便打字。实际的错误是: g ++ -c -o template.o template.cpp template.cpp:14:error:ambiguous template specialization'get <>'for'void * Test :: get(const int&)' template .cpp:7:error:candidate are:void * Test :: get(const int&)[with T = int,S = double] template.cpp:6:error:template void * Test :: get(const T1&)[与T1 = T1,T = INT,S =双倍] –
Venkatesan
2010-08-28 07:23:48
糟糕,我没有看到'typedef',对不起。 – 2010-08-28 07:26:06