I am reading C++ primer book and completely don't understand one line:
using int_array = int[4]; typedef int int_array[4]; // This line for (int_array *p = ia; p != ia + 3; ++p) { for (int *q = *p; q != *p + 4; ++q) cout << *q << ' '; cout << endl; }
Ok typedef
is same as using
. Does it mean int[4][4]
is now int
and how to understand that? And what type is int_array
in for
loop?
Thanks
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.
In C++, 'using' and 'typedef' performs the same task of declaring the type alias. There is no major difference between the two. 'Using' in C++ is considered to define the type synonyms.
Syntax. Note that a typedef declaration does not create types. It creates synonyms for existing types, or names for types that could be specified in other ways.
Alias-declaration or type-alias with 'using' statement offers a more flexible way to define type aliases than typedef, mainly because of alias templates.
Both are doing the exact same thing: Defining int_array
as an alias of an array of 4 int
s
using
has a nice A = B notation that is generally much easier to understand.
using alias = type;
typedef
's notation is not quite backward. For a simple typedef
typedef type alias;
but more complicated typedef
s tend to sprawl. I suspect the syntax was modeled after how one would define a variable, but I can't find where I packed my copy of the old K&R C programming book and can't look that up at the moment.
int int_array[4];
would define int_array
to be an array of 4 int
s. Slapping typedef
on the front
typedef int int_array[4];
makes int_array
a type alias instead of a variable.
Another example,
int * intp;
Defines intp
to be a pointer to an int
.
typedef int * intp;
Defines intp
to be an alias to the type pointer to an int
.
This gets ugly with more complicated data types as the name of the typedef
ed alias may be buried somewhere in the middle of the definition. A typedef
ed function pointer for example:
typedef void (*funcp)(param_t param1, param_t param2, ...);
vs using
using funcp = void (*)(param_t param1, param_t param2, ...);
If you want a 2D array you could
using int_array2D = int[4][4];
or you could define an array of int_array
using int_array2D = int_array[4];
And of course that means you can
using int_array3D = int_array2D[4];
and keep on going until the cows come home or you've packed on so many dimensions that The Doctor's brain melts.
The lines
using int_array = int[4];
and
typedef int int_array[4];
do the same thing. See reference for using and typedef. You can leave one or the other out and the behaviour is the same. It is not an error to have two different declarations, as they are not conflicting (they do the exact same thing).
The first way of doing it (using the using
keyword) was introduced with C++11 and is in my opinion easier to read, so I prefer it over the typedef version.
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