0

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...

1 Answer 1

5

Just add a * to the function alias

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

Live demo

Sign up to request clarification or add additional context in comments.

3 Comments

std::add_pointer is worth mentioning IMHO, although it does essentially the same thing (en.cppreference.com/w/cpp/types/add_pointer)
@zahir add_pointer was mentioned in a now deleted answer :-) (you need 10k rep to see it)
@zahir I think std::add_pointer is the way I'd want to see it in code handed to me. That makes it 100% clear exactly what the code is supposed to do w/o needing to wonder if this is yet another weird thing with * and function typedefs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.