私はここで質問に対するlitbの回答を読んでいました。そこでは、クラステンプレートの特殊なフレンド関数を作成する方法について詳しく説明しています。
私は彼が提案したことを実行するエグザンプラを作成しようとしました(最後のコード):
// use '<>' to specialize the function template with the class template's type
friend std::ostream& operator<< <>(std::ostream& os, const foo<T>& f)
コンパイラエラーが発生します:
error: defining explicit specialization ‘operator<< <>’ in friend declaration
スペシャライゼーションでテンプレートパラメータを明示的に宣言することも機能しません。
friend std::ostream& operator<< <T>(std::ostream& os, const foo<T>& f) // same error
一方、特殊化の使用からフレンド関数テンプレートの使用に変更すると、代わりに機能します。
template<typename U>
friend std::ostream& operator<<(std::ostream& os, const foo<U>& f) // this works
だから私の質問は:
- 最初のエラーの原因は何ですか?
ostream operator
周囲のクラステンプレートの特殊化のためにを明示的に特殊化するにはどうすればよいですか?
以下の模範的なコード:
#include <iostream>
// fwd declarations
template<typename T> struct foo;
template<typename T> std::ostream& operator<<(std::ostream&, const foo<T>&);
template<typename T>
struct foo
{
foo(T val)
: _val(val)
{}
friend std::ostream& operator<< <>(std::ostream& os, const foo<T>& f) // error line
//template<typename U>
//friend std::ostream& operator<<(std::ostream& os, const foo<U>& f) // this works
{
return os << "val=" << f._val;
}
T _val;
};
int main()
{
foo<std::string> f("hello world");
std::cout << f << std::endl;
exit(0);
}