SunStudioでテンプレートクラスにテンプレートフレンドを与えることに問題があります。コードはGNUG++(4.4.1および4.4.3)で正常にコンパイルされますが、Sun Studio C ++(5.9 SunOS_sparc Patch 124863-01 2007/07/25)では失敗します。
最小限の例を次に示します。
// Forward declarations
template<class T> class M;
template<class T> void f(M<T>, M<T>);
// Define M<T>
template<class T>
class M
{
public:
void f(M<T>) { }
friend void ::f<>(M<T>, M<T>);
};
// Define global function f
template<class T>
void f(M<T> a, M<T> b)
{
a.f(b);
}
M<int> a;
を介してコンパイルしようとするとCC -c -o t3.o t3.cpp
、次のエラーメッセージが表示されます。
"t3.cpp", line 12: Warning: A friend function with template-id name must have a template declaration in the nearest namespace.
"t3.cpp", line 22: Where: While specializing "M<int>".
"t3.cpp", line 22: Where: Specialized in non-template code.
"t3.cpp", line 12: Error: Global scope has no declaration for "f".
"t3.cpp", line 22: Where: While specializing "M<int>".
"t3.cpp", line 22: Where: Specialized in non-template code.
1 Error(s) and 1 Warning(s) detected.
これはSunStudioC ++の問題ですか、それとも無効なC ++(GCCで引き続き受け入れられ、警告を表示しません-Wall -pedantic
)ですか?標準に準拠し、GCCとSun Studioの両方でコンパイルされるようにコードを変更するエレガントな方法はありますか?
よろしくお願いします!