4 ms·
It's not that you couldn't do that, it's that polymorphism (in the interface/virtual functions sense) is only one of several paradigms for abstraction/code reus
by MauranKilom 3y ago
It's not that you couldn't do that, it's that polymorphism (in the interface/virtual functions sense) is only one of several paradigms for abstraction/code reuse in C++. And it is on the higher side of runtime cost among them.
Also, the type system in C++, despite all the template stuff, is not actually very good for serious functional programming. For example, when taking a function, there is no generic way to specify its signature in your own signature (and no, taking a std::function is not generic). `require` goes a long way though nowadays.
- injidup 3y agoYes requires is pretty powerful with std::invokable but if one of the parameters of your passed function is also generic then it gets more hairy. template <typename T> requires std::invokable_r_v<int,int,int> int foo(T fn){ return fn(10, 20); }
- gpderetta 3y agoint foo(invocable<int,int> auto fn){ return fn(10,20); } But I think you wanted to show a different example.
- injidup 3y agoI'm on mobile so maybe I mixed the concept and the trait but there should be an invocable_r version that constrains the return type in addition to invocable which just constrains the arguments. And yes the inline version rather than requires is much nicer.
- gpderetta 3y agoOh, I see. Yes, out of the box there is a trait, but not a concept.