rsgmon
rsgmon

Reputation: 1932

What does this typedef statement mean?

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?

Upvotes: 77

Views: 4624

Answers (5)

SameOldNick
SameOldNick

Reputation: 2457

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

Upvotes: 0

Amarghosh
Amarghosh

Reputation: 59471

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

Upvotes: 32

Yu Hao
Yu Hao

Reputation: 122493

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

Upvotes: 42

Harsh
Harsh

Reputation: 2908

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 : C++ - meaning of a statement combining typedef and typename

Upvotes: -7

Mike Seymour
Mike Seymour

Reputation: 254751

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

Upvotes: 96

Related Questions