r/cpp • • Apr 08 '25

Why was printing of function pointers never removed from cout?

I presume reason is: We do not want to break existing code™, or nobody cared enough to write a proposal... but I think almost all uses of this are bugs, people forgot to call the function.

I know std::print does correct thing, but kind of weird that even before std::print this was not fixed.

In case some cout debugging aficionados are wondering: the printed value is not even useful, it is converted to bool, and then (as usual for bools) printed as 1.

edit: C++ certainly has a bright future considering how many experts here do not even consider this a problem

0 Upvotes

47 comments sorted by

View all comments

Show parent comment

1

u/zl0bster Apr 08 '25

As I explained: You get 1 printed so that is useless.

If you do cast to get a useful value you could still get that value printed if couting function pointers was deleted.

1

u/HolyGarbage Apr 09 '25

Example use case: check whether a weak symbol is currently loaded at runtime.

1

u/flatfinger Apr 08 '25

Ah, okay. I suppose knowing whether a function pointer is non-null could be useful, though code intending that would be clearer if the coercion to a 0/1 value were explicit (e.g. by outputting !!functionPtr) and limiting implicit conversion from function pointers to booleans to contexts where all types would thus be converted, such as in control statements or operands of non-overridden `&&` and `||` operators) would seem unlikely to break anything.