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...)
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 ".
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.
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With