From 44c4b29de47d4e9b888b14c98d8718d1dba803b8 Mon Sep 17 00:00:00 2001 From: Date: Wed, 9 Jan 2013 13:59:45 +0000 Subject: [PATCH] Fix glog/stl_logging for clang git-svn-id: https://google-glog.googlecode.com/svn/trunk@118 eb4d4688-79bd-11dd-afb4-1d65580434c0 --- src/glog/stl_logging.h.in | 91 ++++++++++++++++++++-------------- src/windows/glog/stl_logging.h | 91 ++++++++++++++++++++-------------- 2 files changed, 108 insertions(+), 74 deletions(-) diff --git a/src/glog/stl_logging.h.in b/src/glog/stl_logging.h.in index 70e61f0..30836de 100644 --- a/src/glog/stl_logging.h.in +++ b/src/glog/stl_logging.h.in @@ -34,27 +34,6 @@ // LOG(INFO) << "data: " << x; // vector v1, v2; // CHECK_EQ(v1, v2); -// -// Note that if you want to use these operators from the non-global namespace, -// you may get an error since they are not in namespace std (and they are not -// in namespace std since that would result in undefined behavior). You may -// need to write -// -// using ::operator<<; -// -// to fix these errors. -// -// -// Notice for clang users: you should include stl_logging.h BEFORE you -// include logging.h. Otherwise, you will see some errors when you use -// CHECK with STL containers. -// -// Clang's "using ::operator<<" incorporate symbols which are declared -// before the using keyword. With GCC, symbols defined after the using -// keyword are incorporated as well. The CHECK macro defined in -// logging.h uses the using keyword so you need to include logging.h -// after including stl_logging.h. -// #ifndef UTIL_GTL_STL_LOGGING_INL_H_ #define UTIL_GTL_STL_LOGGING_INL_H_ @@ -77,27 +56,16 @@ # include #endif +// Forward declare these two, and define them after all the container streams +// operators so that we can recurse from pair -> container -> container -> pair +// properly. template -inline std::ostream& operator<<(std::ostream& out, - const std::pair& p) { - out << '(' << p.first << ", " << p.second << ')'; - return out; -} +std::ostream& operator<<(std::ostream& out, const std::pair& p); @ac_google_start_namespace@ template -inline void PrintSequence(std::ostream& out, Iter begin, Iter end) { - using ::operator<<; - // Output at most 100 elements -- appropriate if used for logging. - for (int i = 0; begin != end && i < 100; ++i, ++begin) { - if (i > 0) out << ' '; - out << *begin; - } - if (begin != end) { - out << " ..."; - } -} +void PrintSequence(std::ostream& out, Iter begin, Iter end); @ac_google_end_namespace@ @@ -163,4 +131,53 @@ OUTPUT_FIVE_ARG_CONTAINER(__gnu_cxx::hash_multimap) #undef OUTPUT_FIVE_ARG_CONTAINER +template +inline std::ostream& operator<<(std::ostream& out, + const std::pair& p) { + out << '(' << p.first << ", " << p.second << ')'; + return out; +} + +@ac_google_start_namespace@ + +template +inline void PrintSequence(std::ostream& out, Iter begin, Iter end) { + // Output at most 100 elements -- appropriate if used for logging. + for (int i = 0; begin != end && i < 100; ++i, ++begin) { + if (i > 0) out << ' '; + out << *begin; + } + if (begin != end) { + out << " ..."; + } +} + +@ac_google_end_namespace@ + +// Note that this is technically undefined behavior! We are adding things into +// the std namespace for a reason though -- we are providing new operations on +// types which are themselves defined with this namespace. Without this, these +// operator overloads cannot be found via ADL. If these definitions are not +// found via ADL, they must be #included before they're used, which requires +// this header to be included before apparently independent other headers. +// +// For example, base/logging.h defines various template functions to implement +// CHECK_EQ(x, y) and stream x and y into the log in the event the check fails. +// It does so via the function template MakeCheckOpValueString: +// template +// void MakeCheckOpValueString(strstream* ss, const T& v) { +// (*ss) << v; +// } +// Because 'glog/logging.h' is included before 'glog/stl_logging.h', +// subsequent CHECK_EQ(v1, v2) for vector<...> typed variable v1 and v2 can only +// find these operator definitions via ADL. +// +// Even this solution has problems -- it may pull unintended operators into the +// namespace as well, allowing them to also be found via ADL, and creating code +// that only works with a particular order of includes. Long term, we need to +// move all of the *definitions* into namespace std, bet we need to ensure no +// one references them first. This lets us take that step. We cannot define them +// in both because that would create ambiguous overloads when both are found. +namespace std { using ::operator<<; } + #endif // UTIL_GTL_STL_LOGGING_INL_H_ diff --git a/src/windows/glog/stl_logging.h b/src/windows/glog/stl_logging.h index 72c020b..d2e7495 100755 --- a/src/windows/glog/stl_logging.h +++ b/src/windows/glog/stl_logging.h @@ -38,27 +38,6 @@ // LOG(INFO) << "data: " << x; // vector v1, v2; // CHECK_EQ(v1, v2); -// -// Note that if you want to use these operators from the non-global namespace, -// you may get an error since they are not in namespace std (and they are not -// in namespace std since that would result in undefined behavior). You may -// need to write -// -// using ::operator<<; -// -// to fix these errors. -// -// -// Notice for clang users: you should include stl_logging.h BEFORE you -// include logging.h. Otherwise, you will see some errors when you use -// CHECK with STL containers. -// -// Clang's "using ::operator<<" incorporate symbols which are declared -// before the using keyword. With GCC, symbols defined after the using -// keyword are incorporated as well. The CHECK macro defined in -// logging.h uses the using keyword so you need to include logging.h -// after including stl_logging.h. -// #ifndef UTIL_GTL_STL_LOGGING_INL_H_ #define UTIL_GTL_STL_LOGGING_INL_H_ @@ -81,27 +60,16 @@ # include #endif +// Forward declare these two, and define them after all the container streams +// operators so that we can recurse from pair -> container -> container -> pair +// properly. template -inline std::ostream& operator<<(std::ostream& out, - const std::pair& p) { - out << '(' << p.first << ", " << p.second << ')'; - return out; -} +std::ostream& operator<<(std::ostream& out, const std::pair& p); namespace google { template -inline void PrintSequence(std::ostream& out, Iter begin, Iter end) { - using ::operator<<; - // Output at most 100 elements -- appropriate if used for logging. - for (int i = 0; begin != end && i < 100; ++i, ++begin) { - if (i > 0) out << ' '; - out << *begin; - } - if (begin != end) { - out << " ..."; - } -} +void PrintSequence(std::ostream& out, Iter begin, Iter end); } @@ -167,4 +135,53 @@ OUTPUT_FIVE_ARG_CONTAINER(__gnu_cxx::hash_multimap) #undef OUTPUT_FIVE_ARG_CONTAINER +template +inline std::ostream& operator<<(std::ostream& out, + const std::pair& p) { + out << '(' << p.first << ", " << p.second << ')'; + return out; +} + +namespace google { + +template +inline void PrintSequence(std::ostream& out, Iter begin, Iter end) { + // Output at most 100 elements -- appropriate if used for logging. + for (int i = 0; begin != end && i < 100; ++i, ++begin) { + if (i > 0) out << ' '; + out << *begin; + } + if (begin != end) { + out << " ..."; + } +} + +} + +// Note that this is technically undefined behavior! We are adding things into +// the std namespace for a reason though -- we are providing new operations on +// types which are themselves defined with this namespace. Without this, these +// operator overloads cannot be found via ADL. If these definitions are not +// found via ADL, they must be #included before they're used, which requires +// this header to be included before apparently independent other headers. +// +// For example, base/logging.h defines various template functions to implement +// CHECK_EQ(x, y) and stream x and y into the log in the event the check fails. +// It does so via the function template MakeCheckOpValueString: +// template +// void MakeCheckOpValueString(strstream* ss, const T& v) { +// (*ss) << v; +// } +// Because 'glog/logging.h' is included before 'glog/stl_logging.h', +// subsequent CHECK_EQ(v1, v2) for vector<...> typed variable v1 and v2 can only +// find these operator definitions via ADL. +// +// Even this solution has problems -- it may pull unintended operators into the +// namespace as well, allowing them to also be found via ADL, and creating code +// that only works with a particular order of includes. Long term, we need to +// move all of the *definitions* into namespace std, bet we need to ensure no +// one references them first. This lets us take that step. We cannot define them +// in both because that would create ambiguous overloads when both are found. +namespace std { using ::operator<<; } + #endif // UTIL_GTL_STL_LOGGING_INL_H_ -- 2.34.1