テンプレートメンバー関数を持つテンプレートクラスがあります。クラスを明示的にインスタンス化して、コンパイルの大幅な速度低下を回避したいと考えています。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 固有の拡張機能であるインラインの先行テンプレートなし)。このコードフラグメントをコンパイルする方法に光を当てることができますか?
更新:これは私が得るエラーです:
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