std::ostream& operator<<(std::ostream& os, const Foo& foo)
SO をブラウジングしているときに、 aまたは aのオーバーロード/定義に関する質問や回答をよく見つけますFoo operator+(const Foo& l, const Foo& r)
。
これらの演算子をいつどのように作成するか (しないか) は知っていますが、そのnamespace
ことについて混乱しています。
次のクラスがある場合:
namespace bar
{
class Foo {};
}
namespace
異なる演算子の定義はどちらに記述すればよいですか?
// Should it be this
namespace bar
{
std::ostream& operator<<(std::ostream& os, const Foo& foo);
}
// Or this ?
namespace std
{
ostream& operator<<(ostream& os, const bar::Foo& foo);
}
// Or this ?
std::ostream& operator<<(std::ostream& os, const bar::Foo& foo);
同じ質問が にも当てはまりますoperator+
。では、ここでの良い習慣とは何ですか?その理由は何ですか?