3 ms·
The new c++ alt function syntax talked about here: https://blog.petrzemek.net/2017/01/17/pros-and-cons-of-alternative-function-syntax-in-cpp/ https://blog.petrz
by TheAdamist 10y ago
The new c++ alt function syntax talked about here:
https://blog.petrzemek.net/2017/01/17/pros-and-cons-of-alternative-function-syntax-in-cpp/ https://blog.petrzemek.net/2017/01/17/pros-and-cons-of-alter...
mentions replacing function declarations for
void (*get_func_on(int i))(int);
with
auto get_func_on(int i) -> void (*)(int);
which looks a lot more readable to me.
- Longhanks 10y agoI'd say this is even more readable: auto get_func_on() -> std::function<void(int)>
- _lm_ 10y agoIt's more readable, but using std::function here introduces a second layer of indirection vs using a plain function pointer. More specifically, std::function's operator() is virtual, and calls into a subclass that's specialized to function pointers of type void(int). The subclass then performs the actual function pointer call.
- gpderetta 10y agoTechnically function::operator() is not virtual (which wouldn't be very useful as std::function has value semantics), but it does runtime dispatching internally using an unspecified mechanism. This can be virtual functions, or, more commonly, an hand rolled vtable. In the last case, if std::function is constructed with a function pointer exactly matching its signature it could in principle avoid the thunk and directly point to the function itself. I don't think most implementations bother. /pedantic