Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualCornstalks

Posted 06 December 2012 - 02:46 PM


Anyways, for a less contrived example of overloading a pointer and an integer:

ostream & operator<<(int);
ostream & operator<<(const void *);
// along with half a million other member overloads and some non-member overloads


Yeah, that one is responsible for this fun piece of code:
#include <iostream>

int main() {
  volatile char s[] = "Hello, world!";
  std::cout << s << '\n';
}

At least recent versions of g++ give you a reasonable warning about this one.

WTF is this magic? My guess is that since there's no volatile overload it tries to find a conversion for s that matches one of the overloads, which happens to be bool (since s degenerates to a volatile char*, and since it's not null it converts to true)? Am I even close?

#1Cornstalks

Posted 06 December 2012 - 02:45 PM


Anyways, for a less contrived example of overloading a pointer and an integer:

ostream & operator<<(int);
ostream & operator<<(const void *);
// along with half a million other member overloads and some non-member overloads


Yeah, that one is responsible for this fun piece of code:
#include <iostream>

int main() {
  volatile char s[] = "Hello, world!";
  std::cout << s << '\n';
}

At least recent versions of g++ give you a reasonable warning about this one.

WTF is this magic? My guess is that since there's no volatile overload it tries to find a conversion for s that matches one of the overloads, which happens to be bool (since s degenerates to a char*, and since it's not null it converts to true)? Am I even close?

PARTNERS