Re:How Do I Declare a Function Pointer in C?
by Const-me on Thursday January 19, 2017 @02:43PM
(#13438847)
In C++, pointers to member functions are even more cumbersome than C function pointers.
[ Parent
| on HN ]
Re:How Do I Declare a Function Pointer in C?
by kllrnohj on Thursday January 19, 2017 @04:00PM
(#13439443)
auto 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);
Re:How Do I Declare a Function Pointer in C?
by Const-me on Thursday January 19, 2017 @05:48PM
(#13440184)
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.
And std::function introduce some performance overhead.
Re:How Do I Declare a Function Pointer in C?
by kllrnohj on Thursday January 19, 2017 @07:50PM
(#13440882)
> 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.