Question

Can one cleanly make a function pointer typedef from a matching function typedef?

// Can I define fx_pt in terms of fx_t without duplicating the signature?
// Does decltype allow for any new sort of trick here?
using fx_t  = char(int, char, std::vector<float> const &);
using fxp_t = char(*)(int, char, std::vector<float> const &);

// Using function typedef to enforce interface for declarations and definitions.
fx_t foo, bar, baz;
char foo(int) { /* ... */ }

// Using fp typedef elsewhere as normal.
fxp_t whatever{bar};
whatever(99);

I don't ever expect anything about function (pointer) typedef syntax to be completely intuitive for me, but I've not been able to determine whether eliminating the duplicate code is possible.

This may tie into the bigger issue of converting between class/primitive typedefs and class/primitive pointer typedefs, which I recall hearing are not portably converted between...

Was it helpful?

Solution

Just add a * to the function alias

using fx_t  = char(int, char, std::vector<float> const &);
using fxp_t = fx_t*;

Live demo

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top