Which namespace does operator<< (stream) go to?

后端 未结 4 1023
慢半拍i
慢半拍i 2021-02-19 06:01

If I have have some overloaded ostream operators, defined for library local objects, is its okay for them to go to std namespace? If I do not declare them in std namespace, then

4条回答
  •  攒了一身酷
    2021-02-19 06:38

    operator<<( ..., MyClass ) should go in the same namespace as MyClass. You should think of it as part of the interface of MyClass, even though it happens to be (necessarily) a non-member function.

    A couple of references:

    • My article What's In a Class?.
    • C++ Coding Standards Item 57: "Keep a type and its nonmember function interface in the same namespace."

提交回复
热议问题