Using c++ typedef/using type alias

Using c++ typedef/using type alias

本文关键字:type alias using c++ typedef Using      更新时间:2023-10-16

我正在阅读C++入门书,完全听不懂一行:

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;
}

好的typedefusing一样。这是否意味着int[4][4]现在int以及如何理解这一点?循环中int_arrayfor什么类型?

谢谢

TL;博士

两者都在做完全相同的事情:将int_array定义为 4ints 数组的别名

理解语法

using有一个很好的A = B表示法,通常更容易理解。

using alias = type;

typedef的记谱法并不完全落后。对于一个简单的typedef

typedef type alias;

但更复杂的typedef往往会蔓延。我怀疑语法是根据定义变量的方式建模的,但我找不到我在哪里打包我的旧K&R C编程书,目前也无法查找。

int int_array[4];

int_array定义为 4ints 的数组。 拍打typedef

typedef int int_array[4];

使int_array成为类型别名而不是变量。

再比如,

int * intp;

intp定义为指向int的指针。

typedef int * intp;

intp定义为指向int的类型指针的别名。

对于更复杂的数据类型,这变得很丑陋,因为typedefed别名的名称可能隐藏在定义中间的某个地方。typedefed 函数指针,例如:

typedef void (*funcp)(param_t param1, param_t param2, ...);

与使用

using funcp = void (*)(param_t param1, param_t param2, ...);

制作 2D 阵列

如果你想要一个2D阵列,你可以

using int_array2D = int[4][4];

或者您可以定义一个int_array数组

using int_array2D = int_array[4];

当然,这意味着您可以

using int_array3D = int_array2D[4];

继续前进,直到奶牛回家,或者你已经挤满了如此多的维度,以至于医生的大脑融化了。

此行不执行任何操作,因为它是多余的

线条

using int_array = int[4];

typedef int int_array[4];

做同样的事情。有关使用和类型定义,请参阅参考。你可以省略一个或另一个,行为是相同的。有两个不同的声明不是错误,因为它们并不冲突(它们执行完全相同的事情(。

第一种方法(使用using关键字(是在 C++11 中引入的,在我看来更容易阅读,所以我更喜欢它而不是 typedef 版本。

两种类型别名相同:


Type alias,aliastemplate(自 C++11 起(:
Type alias 是一个名称,指的是以前定义的类型(类似于 typedef(:

using identifier attr(optional) = type-id ; 
<小时 />

所以你可以使用:

typedef int int_array[4];

或者你可以只使用(与上面相同(:

using int_array = int[4];

当您需要使用4*sizeof(int)步骤寻址内存时,例如,如果系统int大小为 4 字节,则内存步长大小为 4*4=16 字节。 在这种情况下,即使您可以使用int_array *p;++pp前进一个内存步长,例如 16 字节。 看:

<小时 />

1- 带using int_array = int[4];的工作样品:

#include <iostream>
using std::cout; using std::endl;
int main()
{
int ia[3][4] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
// a range for to manage the iteration
// use type alias
using int_array = int[4];
for (int_array& p : ia)
for (int q : p)
cout << q << " ";
cout << endl;
// ordinary for loop using subscripts
for (size_t i = 0; i != 3; ++i)
for (size_t j = 0; j != 4; ++j)
cout << ia[i][j] << " ";
cout << endl;
// using pointers.
// use type alias
for (int_array* p = ia; p != ia + 3; ++p)
for (int *q = *p; q != *p + 4; ++q)
cout << *q << " ";
cout << endl;
return 0;
}

输出 1:

0 1 2 3 4 5 6 7 8 9 10 11
0 1 2 3 4 5 6 7 8 9 10 11
0 1 2 3 4 5 6 7 8 9 10 11

2- 使用typedef int int_array[4];的工作样品:

#include <iostream>
using std::cout; using std::endl;
int main()
{
int ia[3][4] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
// a range for to manage the iteration
// use type alias
typedef int int_array[4];
for (int_array& p : ia)
for (int q : p)
cout << q << " ";
cout << endl;
// ordinary for loop using subscripts
for (size_t i = 0; i != 3; ++i)
for (size_t j = 0; j != 4; ++j)
cout << ia[i][j] << " ";
cout << endl;
// using pointers.
// use type alias
for (int_array* p = ia; p != ia + 3; ++p)
for (int *q = *p; q != *p + 4; ++q)
cout << *q << " ";
cout << endl;
return 0;
}

输出 2(相同(:

0 1 2 3 4 5 6 7 8 9 10 11
0 1 2 3 4 5 6 7 8 9 10 11
0 1 2 3 4 5 6 7 8 9 10 11

参考: https://github.com/Mooophy/Cpp-Primer/blob/master/ch03/ex3_44.cpp
注意:使用-std=c++11进行编译/链接。