What does this typedef statement mean?

C++Typedef

C++ Problem Overview


In a C++ reference page they provide some typedef examples and I'm trying to understand what they mean.

// simple typedef
typedef unsigned long mylong;
 
 
// more complicated typedef
typedef int int_t, *intp_t, (&fp)(int, mylong), arr_t[10];

So the simple typedef (the first declaration) I understand.

But what are they declaring with the second one (repeated below)?

typedef int int_t, *intp_t, (&fp)(int, ulong), arr_t[10];

Particularly what does (&fp)(int, mylong) mean?

C++ Solutions


Solution 1 - C++

It's declaring several typedefs at once, just as you can declare several variables at once. They are all types based on int, but some are modified into compound types.

Let's break it into separate declarations:

typedef int int_t;              // simple int
typedef int *intp_t;            // pointer to int
typedef int (&fp)(int, ulong);  // reference to function returning int
typedef int arr_t[10];          // array of 10 ints

Solution 2 - C++

typedef int int_t, *intp_t, (&fp)(int, mylong), arr_t[10];

is equivalent to:

typedef int int_t;
typedef int *intp_t;
typedef int (&fp)(int, mylong);
typedef int arr_t[10];

There is actually a similar example in the C++11 standard:

>### C++11 7.1.3 The typedef specifier > >A typedef-name does not introduce a new type the way a class declaration (9.1) or enum declaration does.Example: after > > typedef int MILES , * KLICKSP ; >the constructions > > MILES distance ; > extern KLICKSP metricp ; >are all correct declarations; the type of distance is int that of metricp is “pointer to int.” —end example

Solution 3 - C++

If you have the cdecl command, you can use it to demystify these declarations.

cdecl> explain int (&fp)(int, char)
declare fp as reference to function (int, char) returning int
cdecl> explain int (*fp)(int, char)
declare fp as pointer to function (int, char) returning int

If you don't have cdecl, you should be able to install it in the usual way (e.g. on Debian-type systems, using sudo apt-get install cdecl).

Solution 4 - C++

The (&fp)(int, mylong) part represents a reference to a function. It is not recommended that programmers use functions in typedef for the very reason you're asking this question. It confuses other people looking at the code.

I'm guessing they use the typedef in something like this:

typedef unsigned long mylong; //for completeness
typedef int (&fp)(int, mylong);
int example(int param1, mylong param2);

int main() {
     fp fp_function = example;
     int x = fp_function(0, 1);
     return 0;
}

int example(int param1, mylong param2) {
     // does stuff here and returns reference
     int x = param1;
     return x;
}

Edited in accordance with Brian's comment:

int(&name)(...) is a function reference called name (the function returns an int)

int &name(...) is a function called name returning a reference to an int

A reference to a function which returns an int reference would look something like this: typedef int &(&fp)(int, mylong) (this compiles in a program, but the behaviour is untested).

Solution 5 - C++

typedef is defining a new type for use in your code, like a shorthand.

typedef typename _MyBase::value_type value_type;
value_type v;
//use v

typename here is letting the compiler know that value_type is a type and not an object inside of _MyBase.

the :: is the scope of the type. It is kind of like "is in" so value_type "is in" _MyBase. or can also be thought of as contains.

Possible duplicate : https://stackoverflow.com/questions/18385418/c-meaning-of-a-statement-combining-typedef-and-typename

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionrsgmonView Question on Stackoverflow
Solution 1 - C++Mike SeymourView Answer on Stackoverflow
Solution 2 - C++Yu HaoView Answer on Stackoverflow
Solution 3 - C++AmarghoshView Answer on Stackoverflow
Solution 4 - C++ub3rst4rView Answer on Stackoverflow
Solution 5 - C++HarshView Answer on Stackoverflow