2015-10-20 8 views
8
namespace nm 
{ 
    class C1 {}; 
    class C2 {}; 
    inline std::ostream& operator << (std::ostream& lhs, std::vector<C1> const&) { return lhs; } 
    inline std::ostream& operator << (std::ostream& lhs, std::vector<C2> const&) { return lhs; } 
} 

using nm::operator<<; 

の宣言を使用してグローバルな、そして両方ではない名前空間nmからつのみoperators <<のを使用する宣言する方法はありますか?(コンクリート署名付き)コンクリート出力オペレータ

+0

'using'宣言の使用は何ですか? –

+0

'using ns_name :: name' using-declarationは、同じクラススコープ、ブロックスコープ、またはネームスペースで宣言されているように、ネームスペースns_nameのシンボル名を非修飾ルックアップにアクセスできるようにします。 –

+0

なぜあなたはそれを行う。あなたの目標は何ですか –

答えて

3

一つの解決策は、独自のネストされた名前空間に各operator<<を置くために、次のようになります。

namespace nm 
{ 
    class C1 {}; 
    class C2 {}; 
    namespace nm1 { 
    inline std::ostream& operator << (std::ostream& lhs, C1 const&) { return lhs; } 
    } 
    namespace nm2 { 
    inline std::ostream& operator << (std::ostream& lhs, C2 const&) { return lhs; } 
    } 
} 

using nm::nm1::operator<<; 

LIVE DEMO

+1

nmのコードを変更できない場合は、 –

+0

そのうちの1つは「インライン」です。 – edmz

+0

コードを制御できれば、2つの 'namespace nm {...}'ブロックの間にusing宣言を置き、2番目のブロックにもう1つの 'operator <<'を宣言することもできます。 –

関連する問題