Why can one cast a std::ostream
to a void
pointer? I am not aware of any such conversion operator in std::ostream
. Code below
#include <iostream>
int main()
{
void *p = std::cout; // why does this work?
}
I'm asking this question since I've seen a placement operator new
invoked as
Foo* pFoo = new (std::cerr) Foo;
and have absolutely no idea why would one write such a thing.
PS: I am compiling with g++ 4.9.2 with or without -std=c++11
. clang++ does not accept the code.
PSS: Found out that due to the so called "safe bool problem" (see @nicebyte's answer), in pre C++11 a void*
conversion operator was defined for std::ostream
, which was then removed in C++11. However, my code compiles fine in C++11 using g++. More than that, clang++ rejects it no matter what version of the standard I use, even with -std=c++98
, although my understanding is that it should accept if compiled as pre-C++11.
new (std::cerr) Foo
? What. The. Hell. – Burcham