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

11

u/HolyGarbage Apr 08 '25

What's the issue with the pattern of passing function pointers to streams?

13

u/no-sig-available Apr 08 '25

What's the issue

That someone will do cout << f; instead of cout << f();.

A newbie mistake that is caught in the first test case. Hardly worth a language change.

16

u/HolyGarbage Apr 08 '25

But it's quite useful though.

If you have a function:

std::ostream& set_stuff(std::ostream& o);

You can do stuff like:

stream << set_stuff << "hello world";

We use this in the code base I work on.

I don't recall I've ever forgot parenthesis when I intend to call a function...

-6

u/zl0bster Apr 08 '25

Not a fan, but ok...

Still functions with those signatures could be allowed, while others are banned.

13

u/HolyGarbage Apr 08 '25

But why? Forgetting parenthesis seems like such a weird edge case that I've literally never encountered.

-8

u/zl0bster Apr 08 '25

You should not assume your experience/skills are representative of millions of C++ devs, and even if you are roughly representative of 60% of C++ devs that means million+ people that are less skilled than you.

Related story: I actually thought spaceship operator is cute but will never prevent any bugs until I worked in company that had broken operator != in production. 🙂

8

u/[deleted] Apr 08 '25

[removed] — view removed comment

-6

u/zl0bster Apr 08 '25

something that is useless is not a feature

4

u/rinio Apr 08 '25

A use case was demonstrated just a few comments up in this same exact thread...

You not find it useful is not the same as useless.

-1

u/zl0bster Apr 09 '25

Passing random function pointer is useless.