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

时间:2022-08-26 15:25:04
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<<;

Is there way to declare to use only one of operators << from namespace nm in the global one, and not both?

是否有方法声明在全局操作中只使用一个<< from namespace nm的操作符,而不是同时使用两个?

1 个解决方案

#1


3  

One solution would be to put each operator<< in its own nested name space:

一种解决方案是将每个操作符<< < <放在其自己的嵌套名称空间中:< p>

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


3  

One solution would be to put each operator<< in its own nested name space:

一种解决方案是将每个操作符<< < <放在其自己的嵌套名称空间中:< p>

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

现场演示