Live data from Hacker News

Callbacks in C++ using template functors (1994)

tutok.sk

21–30 of 51 posts

Re: Callbacks in C++ using template functors (1994)

#21
post #3

C++ was so much cleaner in the 90s, when it was still essentially "C with classes," which is how I like to use the language. Modern standards have turned it into an ugly mess.

A sentence from the article: "Given the extreme undesirability of any new language features I'd hardly propose bound-pointers now." It shows that C++ was considered too complex already in the 90s.

And then they introduced coke at committee meetings, the crazy shit they've been coming up with lately shows absolutely zero understanding of the complexity issue.

Re: Callbacks in C++ using template functors (1994)

#22
post #5

Red flags for me when I see nonstandard functors in a c++ codebase (esp if the "glue" is in a setup function independent of the objects): (i) Have they thought about the relative lifetimes of the sender and receiver? (ii) Is the callback a "critical section" where certain side-effects have undefined behavior? (iii) Does the functors store debugging info that .natvis can use? (iv) Is it reeeeeeeally that bad to just i…

> Red flags for me when I see nonstandard functors in a c++ codebase

Even if it's 1994???

Re: Callbacks in C++ using template functors (1994)

#24
post #21

Earlier quoted context omitted.

A sentence from the article: "Given the extreme undesirability of any new language features I'd hardly propose bound-pointers now." It shows that C++ was considered too complex already in the 90s.

And then they introduced coke at committee meetings, the crazy shit they've been coming up with lately shows absolutely zero understanding of the complexity issue.

and yet client code can be incredibly simpler nowadays thanks to all these features.

I can write this simple struct:

    struct Person {
      std::string name;
      my_complicated_date_type date_of_birth;
    };
and get serialization, network interop, logging, automated UI generation, hashing, type-safe IDs, etc. without having to write an additional line of code. Twenty years ago you had to write 2000 lines of additional boilerplate for each data type to get to the same place.

Re: Callbacks in C++ using template functors (1994)

#25
post #21

Earlier quoted context omitted.

And then they introduced coke at committee meetings, the crazy shit they've been coming up with lately shows absolutely zero understanding of the complexity issue.

and yet client code can be incredibly simpler nowadays thanks to all these features. I can write this simple struct: struct Person { std::string name; my_complicated_date_type date_of_birth; }; and get serialization, network interop, logging, automated UI generation, hashing, type-safe IDs, etc. without having to write an additional line of code. Twenty years ago you had to write 2000 lines of additional boilerplate…

You would have added some codegen, should be possible to write a codegen framework from scratch in 2000 lines even.

Arguably the result would have been easier to read and maintain and not as slow to compile.

Re: Callbacks in C++ using template functors (1994)

#26
post #5

Red flags for me when I see nonstandard functors in a c++ codebase (esp if the "glue" is in a setup function independent of the objects): (i) Have they thought about the relative lifetimes of the sender and receiver? (ii) Is the callback a "critical section" where certain side-effects have undefined behavior? (iii) Does the functors store debugging info that .natvis can use? (iv) Is it reeeeeeeally that bad to just i…

> Red flags for me when I see nonstandard functors in a c++ codebase Even if it's 1994???

Yes in 1994 I had these exact judgements, at age 11 :P

Re: Callbacks in C++ using template functors (1994)

#27
post #3

C++ was so much cleaner in the 90s, when it was still essentially "C with classes," which is how I like to use the language. Modern standards have turned it into an ugly mess.

>ugly mess That may be the case, but there are plenty of examples of elegant implementations. JUCE, for instance: #include class MyComponent { public: void doAsyncOperation(std::function callback) { // Simulate async work juce::MessageManager::callAsync([callback]() { callback(42); // Call the functor with result }); } }; // Usage MyComponent comp; comp.doAsyncOperation([](int result) { juce::Logger::writeToLog("Call…

Well, that definitely doesn't look "clean and readable" to me for whatever that's worth.

Re: Callbacks in C++ using template functors (1994)

#29

Doing this today I'd just have a std::function parameter and have callers pass in a lambda. I may use a third party std::function that doesn't have the weird copy semantics though

> I may use a third party std::function that doesn't have the weird copy semantics though

Note that C++23 brings std::move_only_function if you're storing a callback for later use, as well as std::function_ref if you don't need lifetime extension.

https://en.cppreference.com/w/cpp/utility/functional/move_on...

Re: Callbacks in C++ using template functors (1994)

#30
post #21

Earlier quoted context omitted.

And then they introduced coke at committee meetings, the crazy shit they've been coming up with lately shows absolutely zero understanding of the complexity issue.

and yet client code can be incredibly simpler nowadays thanks to all these features. I can write this simple struct: struct Person { std::string name; my_complicated_date_type date_of_birth; }; and get serialization, network interop, logging, automated UI generation, hashing, type-safe IDs, etc. without having to write an additional line of code. Twenty years ago you had to write 2000 lines of additional boilerplate…

As some not into modern c++, how would you get all functionality? Is there any guide or documentation yout could point to?. It is fascinating if all that could be done with just the struct defenition.
Post reply on HN