使用具体输出操作符的声明(带有具体签名)

Ром*_*тев 8 c++ namespaces

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<<;
Run Code Online (Sandbox Code Playgroud)

有没有办法声明在全局operators <<命名空间nm中只使用命名空间中的一个,而不是两个?

101*_*010 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<<;
Run Code Online (Sandbox Code Playgroud)

现场演示