c++ - 类型别名和相似类型

标签 c++ c++11

关于cpp reference如果忽略顶级 cv 限定符,则可以说两个指针是相同的,它们指向相同的类型。示例中还有这个示例

int (* const *)(int *) and int (* volatile *)(int *) are similar; 

我想知道这些应该如何相似? AFAIK 这些表示指向函数指针的指针,为了简化它们可以重写为

 using XYZ = int(*)(int *);
 const XYZ *;     // A
 volatile XYZ *;  // B

在这种情况下,A 和 B 指针不是指向不同的类型吗?

最佳答案

On cpp reference it is said that two pointers are the same if ignoring the top level cv qualifiers they point to the same type

不完全是。它说:

Informally, two types are similar if, ignoring top-level cv-qualification:

  • they are the same type; or

  • they are both pointers, and the pointed-to types are similar; or

请注意,相同相似不同。


const XYZ *;     // A
volatile XYZ *;  // B

Aren't A and B pointers to different types in that case?

它们确实是不同的类型。但它们很相似。

关于c++ - 类型别名和相似类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58268296/

相关文章:

C++ 非多态接口(interface)

c++ - 使用 dynamic_pointer_cast 通过引用获取 shared_ptr

c++ - 将函数指针转换为 void(*)(),然后重新转换为原始类型

c++ - 纯虚函数会阻止隐式生成的 move 构造函数吗?

c++ - dlopen 可能出现段错误的潜在原因?

c++ - CreatePolygonRgn 和 const POINT *

c++ - 在纯 C++ (C++11) 中扩充一个类/应用一个方面

c++ - 如何分隔 C++ 字符串中的数字和字母?

C++14 类型列表,有什么理由更喜欢 'free functions' 到 'methods',反之亦然?

c++ - 为什么 CMake 使用 Opencv 未正确设置包含目录(MSVC 2010 项目)