Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this typedef definition mean?

Tags:

c++

typedef

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

like image 305
Michael Avatar asked Jan 06 '14 18:01

Michael


People also ask

What is typedef example?

typedef struct { int scruples; int drams; int grains; } WEIGHT; The structure WEIGHT can then be used in the following declarations: WEIGHT chicken, cow, horse, whale; In the following example, the type of yds is "pointer to function with no parameter specified, returning int ".

What does typedef stand for?

As the name itself suggests, typedef stands for “type definition”. typedef is nothing but a way to assign a new name to a pre-existing data type. In other words, typedef is basically a reserved keyword that we use in order to create an alias name for a specific data type.

Why would you use typedef?

The typedef keyword allows the programmer to create new names for types such as int or, more commonly in C++, templated types--it literally stands for "type definition". Typedefs can be used both to provide more clarity to your code and to make it easier to make changes to the underlying data types that you use.

What is typedef and #define in C?

Typedef is a keyword in the C programming language. #define is a pre-processor and used as macro used in C programming. 2. It is a keyword used to provide an alternate name to the existing data types only. And that name can be used to initialize the variables in a program.


3 Answers

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.

like image 88
Lightness Races in Orbit Avatar answered Sep 28 '22 08:09

Lightness Races in Orbit


The first typedef creates an alias for a function which takes 2 parameters and returns a pointer-to-n.

The second typedef creates an alias for a pointer-to-function which takes 2 parameters and returns an n by value.

like image 24
John Dibling Avatar answered Sep 28 '22 06:09

John Dibling


In the first case the typedef defines an alias for a function type that has two parameters of type const n *and return type n *

In the second case instead of the function type there is a declaration of a function pointer having return type n.

In the first case you could write also for example

typedef n * ( (m)(const n*, const n*) );

It is equivalent to your typedef.

As for the usage you can use it as a function declaration. For exmaple

m MyFunc;

Another example

struct A
{
   typedef int n;
   typedef n  ( Operation )( n, n ) const;
   Operation Add;
   Operation Subtract;
   Operation Division;
   Operation Multiply;
};

// and below the function definitions
like image 40
Vlad from Moscow Avatar answered Sep 28 '22 07:09

Vlad from Moscow