What does this typedef definition mean?

c++, typedef

Solution

I asked geordi to win me some rep:

<tomalak> << TYPE_DESC<m>; struct n {}; typedef n *(m)(const n*, const n*);
<geordi> function taking 2 pointers to constant ns and returning a pointer to a n

C type declaration syntax is horrible and it does become particularly apparent when you start doing complex declarations like this. Notice how the return type and arguments are written around the `m`, not the `n`, which is totally backwards to intuition since it's `m` that you're creating.

Your second example is:

<tomalak> << TYPE_DESC<m>; struct n {}; typedef n (*m)(const n*, const n*);
<geordi> pointer to a function taking 2 pointers to constant ns and returning a n

By moving the `*`, you are no longer applying it to the function type's return type, but to the function type itself.

In C++11, unless you have a desperate need for your calls to be hyper-efficient, please stick to the following, for the love of Cthulhu! :-)

typedef std::function<n*(const n*, const n*)> m;

If you wish to stick to function pointers then you can:

using m = n*(const n*, const n*);

Prior to that you can use `boost::function` or learn the horrific C declarator rules. It is true that you should know them; it's just that hopefully you won't have to use them too often.

Problem

I have seen the following (C++): ``` typedef n *(m)(const n*, const n*); ``` What does it mean and how can it be used? I understand this: ``` typedef n (*myFunctP)(const n*, const n*); ``` but what is the difference to the typedef above? (Hope this is no duplicate, did not find something similar...)

Original source

Related problems