Skip to content

Instantly share code, notes, and snippets.

@martinmoene
Last active December 16, 2015 10:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinmoene/5418947 to your computer and use it in GitHub Desktop.
Save martinmoene/5418947 to your computer and use it in GitHub Desktop.
Check for global operator<< for GCC 4.7.2, lang 3.1, VC2010, VC8, VC6
// http://stackoverflow.com/a/5771273/437272
#include <iosfwd>
namespace detail {
typedef char no;
typedef char yes[2];
struct any_t {
template<typename T> any_t( T const& );
};
no operator<<( std::ostream const&, any_t const& );
yes& test( std::ostream& );
no test( no );
// VC6 requires name to differ from parent class
template<typename T>
struct has_insertion_operator_impl {
static std::ostream &s;
static T const &t;
enum { value = sizeof( test(s << t) ) == sizeof( yes ) };
};
}
template<typename T>
struct has_insertion_operator : detail::has_insertion_operator_impl<T> {};
// --
#include <iostream>
struct S{};
struct X{};
std::ostream & operator << (std::ostream & os, S const & ) { return os;}
int main(void)
{
std::cout << has_insertion_operator<int>::value << std::endl;
std::cout << has_insertion_operator< S >::value << std::endl;
std::cout << has_insertion_operator< X >::value << std::endl;
}
// cl -nologo -EHsc InsertionOpDetector-vc6.cpp && InsertionOpDetector-vc6
// g++ -o InsertionOpDetector-vc6.exe InsertionOpDetector-vc6.cpp && InsertionOpDetector-vc6
// C:\Programs\mingw-clang-3.1\bin\clang++ -o InsertionOpDetector-vc6.exe InsertionOpDetector-vc6.cpp && InsertionOpDetector-vc6
// works with
// GCC 4.7.2
// clang 3.1
// VS2010 (VC10), VS2005 (VC8), VS6 (VC6)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment