Test for the existence of the std :: ostream << operator through an SFINAE GCC error?

I decided to try my hand at a bit The replacement error code is not an error (SFINAE) to check if a global operator<<is defined for a custom type.

The overflow question SFINAE + sizeof = detects if compiling the expression already handles testing for the statement <<through SFINAE, but my code is slightly different and produces a puzzling result.

In particular, my test code below will not even compile if I try to define operator<<for my user type (struct A) after the test_ostrSFINAE template code, but, in my opinion, it should work fine, because it is defined before any actual instance of the test_ostr class.

OTOH , it compiles if I define operator<<for another class that is not even created or not defined. But then the code test_ostrcould not find correctly operator<<.

This code compiles and runs in GCC 4.4.3:

//#define BUG 1 // Uncomment and the program will not compile in GCC 4.4.3
//#define BUG 2 // Uncomment and the program will compile, but produces an incorrect result, claiming operator<< is not defined for A.

#include <iostream>

struct A{};
struct B{};

// If BUG is #defined, the operator<< for struct A will be defined AFTER the test_ostr code
// and if BUG <=1, then GCC 4.4.3 will not compile with the error:
// sfinae_bug.cpp:28: error: template argument 2 is invalid
#ifdef BUG
// if BUG > 1, defining the opertor << for *C*, an un-defined type, will make GCC  magically compile!?
#  if BUG > 1
  struct C;
  std::ostream& operator<<(std::ostream&, const C&);
#  endif
#endif

#ifndef BUG
  std::ostream& operator<<(std::ostream& ostr, const A&) { return ostr; };
#endif

template<class T>
struct test_ostr
{
  template <class U, std::ostream& (*)(std::ostream&, const U&) >
  struct ostrfn;
  template<class U>
  static short sfinae(ostrfn<U, &operator<< >*);
  template<class U>
  static char  sfinae(...);
  enum { VALUE = sizeof(sfinae<T>(0)) - 1 };
};

#ifdef BUG
  std::ostream& operator<<(std::ostream& ostr, const A&) { return ostr; };
#endif

int main(void)
{
  std::cout << "std::ostream defined for A: " << int(test_ostr<A>::VALUE) << std::endl;
  std::cout << "std::ostream defined for B: " << int(test_ostr<B>::VALUE) << std::endl;
  return 0;
}

Result showing errors:

>c++ sfinae_bug.cpp && ./a.out 
std::ostream defined for A: 1
std::ostream defined for B: 0

>c++ -DBUG sfinae_bug.cpp && ./a.out 
sfinae_bug.cpp:28: error: template argument 2 is invalid

>c++ -DBUG=2 sfinae_bug.cpp && ./a.out 
std::ostream defined for A: 0
std::ostream defined for B: 0

Are these compiler errors? Am I missing something? Are the results different with another compiler?

+3
1

, operator<< . , operator<<, , .

template<class U>
static short sfinae(ostrfn<U, &operator<< >*);

SFINAE , .

+3

All Articles