Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typedef declaration in the form `int typedef my_int;`

Tags:

c++

typedef

To declare my_int as a type alias for int we can write:

typedef int my_int;   // (1)

Curiously, the following also seems to define an int alias:

int typedef my_int;   // (2)

I have never seen such syntax before. Why does it work?

like image 873
Evg Avatar asked Nov 09 '19 17:11

Evg


People also ask

What is typedef declaration?

A typedef declaration is a declaration with typedef as the storage class. The declarator becomes a new type. You can use typedef declarations to construct shorter or more meaningful names for types already defined by C or for types that you have declared.

What is typedef int?

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 the typedef declaration give suitable example?

Formally, if the typedef declaration defines an unnamed class or enum, the first typedef-name declared by the declaration to be that class type or enum type is used to denote the class type or enum type for linkage purposes only. For example, in typedef struct { /* ... */ } S;, S is a typedef-name for linkage purposes.

What is the use of typedef statement?

typedef is a reserved keyword in the programming languages C and C++. It is used to create an additional name (alias) for another data type, but does not create a new type, except in the obscure case of a qualified typedef of an array type where the typedef qualifiers are transferred to the array element type.


2 Answers

My reasoning after reading C++ reference is this: (1) and (2) are declarations of the form

specifiers-and-qualifiers declarators-and-initializers;

with specifiers-and-qualifiers being either typedef int or int typedef.

The order of specifiers and qualifiers doesn't matter, and both (1) and (2) are valid declarations of a type alias. For example, to define an alias for const int we can, in principle, use any of these 6 combinations:

typedef int const my_cint;
typedef const int my_cint;
int typedef const my_cint;
const typedef int my_cint;
int const typedef my_cint;
const int typedef my_cint;
like image 56
Evg Avatar answered Sep 19 '22 07:09

Evg


Indeed, your interpretation is correct as explained here:

The typedef specifier, when used in a declaration's decl-specifier-seq, specifies that the declaration is a typedef declaration, and declares typedef-names rather than functions or objects.

Nevertheless, in modern c++, the type alias are better defined with a using clause:

using my_int = int; 

It's not just a matter of style: typedef don't support templatization whereas type alias do:

template <typename T>
using my_list = list<T>;   // not possible with typedef
...
my_list<double> numbers; 
like image 37
Christophe Avatar answered Sep 19 '22 07:09

Christophe