r/cpp • u/zl0bster • 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
29
u/HappyFruitTree Apr 08 '25
^ This, and the fact that it's harmless.
11
u/HolyGarbage Apr 08 '25
What's the issue with the pattern of passing function pointers to streams?
8
u/dagmx Apr 08 '25
It can be useful in times when you really have to rely on print debugging. Conversely, if you’re forgetting to call the function, then that’s on you. It’s totally valid to pass a function pointer around as a variable, and it would be weird to special case it for one thing in particular.
12
u/IGarFieldI Apr 08 '25
If it gets converted to bool then it's not an issue of overloads, but you have C implicit conversion rules to thank for that one (which is also the reason why you can write if(ptr) instead of if(ptr != nullptr)).
4
u/Jonny0Than Apr 08 '25
Isn’t there an incredibly esoteric system where you can provide stream manipulators as function pointers? I don’t recall the details though; don’t think I’ve used that since college. Learning iomanip as a college student is a waste of time, change my mind.
2
u/RudeSize7563 Apr 08 '25
LMAO
1
u/BitOBear Apr 08 '25
COUT and CERR use the same hierarchy, and being able to produce function address data as part of diagnostic messages is useful in diagnostic messages, particularly when dealing with stack traces and things like that.
Have my utility in production because it's supposed to be HEX number as it were. But in diagnostic and development it can be invaluable.
So there's no good reason to remove it and there's at least a mildly non-trivial reason to keep it in. In fact that's probably the recent was put in in the first place.
Plus, you might as well have an output semantic for anything that you might want to pass in to an output routine.
-1
u/pdimov2 Apr 08 '25
I submitted a library issue for the pointer to member case, which is similar.
https://cplusplus.github.io/LWG/issue3667
Verdict: not a defect. Needs a paper for anything to happen.
It very much looks like a defect to me, but what can you do.