空结构或匿名结构作为标记
Empty struct or anonymous struct as tag
本文关键字:结构 更新时间:2023-10-16
将
标签类型定义为匿名空结构或空结构之间有什么区别吗?
using A = struct {};
struct B {};
在我看来,唯一的区别是"有效"类型名称,当人们使用一种反射(即 __PRETTY_FUNCTION__
、<cxxabi.h>:abi::__cxa_demangle(typeid().name())
等)。
ADL 适用于两种方式:
namespace ns
{
using A = struct {};
struct B {};
constexpr
bool
adl(A)
{
return true;
}
constexpr
bool
adl(B)
{
return true;
}
}
template< typename type >
constexpr
bool
adl(type)
{
return false;
}
static_assert(adl(ns::A{}));
static_assert(adl(ns::B{}));
除了您已经注意到的不同字符串之外,唯一显着的区别是您可以使用精心制作的类型说明符引用B
,因此您可以说struct B b;
而不是B b;
但不能使用struct A a;
A
因为 typedef-name 而不是类名。
然而,几乎没有一个很好的理由说struct B
而不仅仅是B
所以在实践中,差异并不重要,尤其是对于标签类型。
相关文章:
- 如何循环打印顶点结构
- 通过方法访问结构
- 使用不带参数的函数访问结构元素
- 预处理器:插入结构名称中的前一个行号
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 孤立代码块在结构中引发异常
- 有什么方法可以遍历结构吗
- 如何在 C# 中映射双 C 结构指针?
- 如何在C++中使用结构生成映射
- 无法将结构注册为增强几何体3D点
- 多成员Constexpr结构初始化
- C++将文本文件中的数据读取到结构数组中
- 如何重构类层次结构以避免菱形问题
- 如何在C++中序列化结构数据
- std::vector的包装器,使数组的结构看起来像结构的数组
- 没有为自己的结构调用列表推回方法
- 奇怪的结构&GCC&clang(void*返回类型)
- 在 c++ 中拥有一组结构的正确方法是什么?
- vscode g++链路故障:体系结构x86_64的未定义符号
- C++概念:如何使用'concept'检查模板化结构的属性?