From: Kefu Chai Date: Tue, 28 Aug 2018 06:48:28 +0000 (+0800) Subject: include/types: move operator<< into the proper namespace X-Git-Tag: v14.0.1~456^2 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=2f57008980c451d4034a4640bce76a60fe8405eb;p=ceph.git include/types: move operator<< into the proper namespace we should define the operator<< in the namespace of its 2nd parameter. normally, the operator<< defined in the global namespace works. but there is a subtle difference between the operator<< defined in std namespace and the global one because of Koenig lookup. Signed-off-by: Kefu Chai --- diff --git a/src/include/types.h b/src/include/types.h index af3b8d18039bb..1140e3f45a501 100644 --- a/src/include/types.h +++ b/src/include/types.h @@ -95,38 +95,47 @@ typedef off_t loff_t; // the case of containers of containers. I'm tempted to abstract this // stuff using template templates like I did for denc. +namespace std { template -inline ostream& operator<<(ostream&out, const pair& v); +inline std::ostream& operator<<(std::ostream&out, const std::pair& v); template -inline ostream& operator<<(ostream& out, const vector& v); +inline std::ostream& operator<<(std::ostream& out, const std::vector& v); template -inline ostream& operator<<(ostream& out, const deque& v); +inline std::ostream& operator<<(std::ostream& out, const std::deque& v); template -inline ostream& operator<<(ostream& out, const std::tuple &t); -template -inline ostream& operator<<(ostream& out, const boost::tuple &t); +inline std::ostream& operator<<(std::ostream& out, const std::tuple &t); template -inline ostream& operator<<(ostream& out, const list& ilist); +inline std::ostream& operator<<(std::ostream& out, const std::list& ilist); template -inline ostream& operator<<(ostream& out, const set& iset); +inline std::ostream& operator<<(std::ostream& out, const std::set& iset); template -inline ostream& operator<<(ostream& out, const boost::container::flat_set& iset); +inline std::ostream& operator<<(std::ostream& out, const std::multiset& iset); template -inline ostream& operator<<(ostream& out, const boost::container::flat_map& iset); -template -inline ostream& operator<<(ostream& out, const multiset& iset); +inline std::ostream& operator<<(std::ostream& out, const std::map& m); template -inline ostream& operator<<(ostream& out, const map& m); +inline std::ostream& operator<<(std::ostream& out, const std::multimap& m); +} + +namespace boost { +template +inline std::ostream& operator<<(std::ostream& out, const boost::tuple &t); + +namespace container { +template +inline std::ostream& operator<<(std::ostream& out, const boost::container::flat_set& iset); template -inline ostream& operator<<(ostream& out, const multimap& m); +inline std::ostream& operator<<(std::ostream& out, const boost::container::flat_map& iset); +} +} +namespace std { template -inline ostream& operator<<(ostream& out, const pair& v) { +inline std::ostream& operator<<(std::ostream& out, const std::pair& v) { return out << v.first << "," << v.second; } template -inline ostream& operator<<(ostream& out, const vector& v) { +inline std::ostream& operator<<(std::ostream& out, const std::vector& v) { out << "["; for (auto p = v.begin(); p != v.end(); ++p) { if (p != v.begin()) out << ","; @@ -135,8 +144,9 @@ inline ostream& operator<<(ostream& out, const vector& v) { out << "]"; return out; } + template -inline ostream& operator<<(ostream& out, const deque& v) { +inline std::ostream& operator<<(std::ostream& out, const std::deque& v) { out << "<"; for (auto p = v.begin(); p != v.end(); ++p) { if (p != v.begin()) out << ","; @@ -146,15 +156,8 @@ inline ostream& operator<<(ostream& out, const deque& v) { return out; } -template -inline ostream& operator<<(ostream& out, const boost::tuple &t) { - return out << boost::get<0>(t) << "," - << boost::get<1>(t) << "," - << boost::get<2>(t); -} - template -inline ostream& operator<<(ostream& out, const std::tuple &t) { +inline std::ostream& operator<<(std::ostream& out, const std::tuple &t) { auto f = [n = sizeof...(Ts), i = 0, &out](const auto& e) mutable { out << e; if (++i != n) @@ -165,7 +168,7 @@ inline ostream& operator<<(ostream& out, const std::tuple &t) { } template -inline ostream& operator<<(ostream& out, const list& ilist) { +inline std::ostream& operator<<(std::ostream& out, const std::list& ilist) { for (auto it = ilist.begin(); it != ilist.end(); ++it) { @@ -176,7 +179,7 @@ inline ostream& operator<<(ostream& out, const list& ilist) { } template -inline ostream& operator<<(ostream& out, const set& iset) { +inline std::ostream& operator<<(std::ostream& out, const std::set& iset) { for (auto it = iset.begin(); it != iset.end(); ++it) { @@ -187,7 +190,7 @@ inline ostream& operator<<(ostream& out, const set& iset) { } template -inline ostream& operator<<(ostream& out, const boost::container::flat_set& iset) { +inline std::ostream& operator<<(std::ostream& out, const std::multiset& iset) { for (auto it = iset.begin(); it != iset.end(); ++it) { @@ -198,55 +201,68 @@ inline ostream& operator<<(ostream& out, const boost::container::flat_set -inline ostream& operator<<(ostream& out, const boost::container::flat_map& m) { +inline std::ostream& operator<<(std::ostream& out, const std::map& m) +{ + out << "{"; for (auto it = m.begin(); it != m.end(); ++it) { if (it != m.begin()) out << ","; out << it->first << "=" << it->second; } - return out; -} - -template -inline ostream& operator<<(ostream& out, const multiset& iset) { - for (auto it = iset.begin(); - it != iset.end(); - ++it) { - if (it != iset.begin()) out << ","; - out << *it; - } + out << "}"; return out; } template -inline ostream& operator<<(ostream& out, const map& m) +inline std::ostream& operator<<(std::ostream& out, const std::multimap& m) { - out << "{"; + out << "{{"; for (auto it = m.begin(); it != m.end(); ++it) { if (it != m.begin()) out << ","; out << it->first << "=" << it->second; } - out << "}"; + out << "}}"; + return out; +} + +} // namespace std + +namespace boost { +namespace tuples { +template +inline std::ostream& operator<<(std::ostream& out, const boost::tuples::tuple &t) { + return out << boost::get<0>(t) << "," + << boost::get<1>(t) << "," + << boost::get<2>(t); +} +} +namespace container { +template +inline std::ostream& operator<<(std::ostream& out, const boost::container::flat_set& iset) { + for (auto it = iset.begin(); + it != iset.end(); + ++it) { + if (it != iset.begin()) out << ","; + out << *it; + } return out; } template -inline ostream& operator<<(ostream& out, const multimap& m) -{ - out << "{{"; +inline std::ostream& operator<<(std::ostream& out, const boost::container::flat_map& m) { for (auto it = m.begin(); it != m.end(); ++it) { if (it != m.begin()) out << ","; out << it->first << "=" << it->second; } - out << "}}"; return out; } - +} +} // namespace boost