3 ms·
In C++, pointers to member functions are even more cumbersome than C function pointers.
by Const-me 10y ago
In C++, pointers to member functions are even more cumbersome than C function pointers.
- bstamour 10y agoAgreed. I rarely use pmf's, so every time I do I have to look up the syntax again.
- kllrnohj 10y agoauto greet = std::mem_fn(&Foo::display_greeting); Looks pretty simple to me, much simpler than C function pointers. :) Pairs nicely with std::bind, too, like so: Foo foo; std::function<void(int)> setter = std::bind(&Foo::setValue, &foo, std::placeholders::_1); setter(42);
- Const-me 10y agoIf you need to keep that pointer in a collection, or in a class member, or pass as a function argument, “auto” won’t work. And std::function introduce some performance overhead.
- kllrnohj 10y ago> If you need to keep that pointer in a collection, or in a class member, or pass as a function argument, “auto” won’t work. Trivially solved with decltype, no need to remember anything. decltype(std::mem_fn(&Foo::Whatever)) Typedef it if you want. > And std::function introduce some performance overhead. It's the same performance as a virtual function call. Pretty much the same as a function pointer invoke.
- Koshkin 10y agoIndeed, C++ is an excellent language, extremely writable; but readable, it isn't.