c++ - C++ 为什么 typedef 结构与 typedef 原语相比是独一无二的
问题描述
为什么 a typedef
for astruct
被认为是唯一类型,而 a typedef
for astd::string
或其他原语则不是?考虑到您可能有两个struct
s 具有相同的组件,但它被视为两种不同的、不相关的类型。
这是一个工作示例。
#include <iostream>
//#define FAIL
#ifdef FAIL
// These are considered to be the same thing
typedef std::string StringEventA;
typedef std::string StringEventB;
#else
// But these are two entirely different types...
typedef struct {std::string str;} StringEventA;
typedef struct {std::string str;} StringEventB;
#endif
class Foo {
public:
void Handle(StringEventA event){std::cout<<"A\n";}
void Handle(StringEventB event){std::cout<<"B\n";}
};
int main()
{
StringEventA a;
StringEventB b;
Foo foo;
foo.Handle(a);
foo.Handle(b);
return 0;
}
解决方案
struct {std::string str;}
这定义了一个没有名字的类型。
std::string
这命名了一个类型。
typedef TYPE name;
name
这会为 type创建一个别名TYPE
。
在这种struct{}
情况下,它为在该行上创建的其他未命名类型提供了一个名称。
在这种std::string
情况下,它为已命名的事物命名。
因此,在一种情况下,您创建了 2 种类型。 typedef
只给了他们名字。
template<class T, class Tag>
struct strong:T{
using T::T;
};
这是一个合理的强类型定义。
远非完美,但体面。
struct tagA;
struct tagB;
using StringEventA=strong<std::string, tagA>;
using StringEventB=strong<std::stings, tagB>;
标签可以是很多东西,而不仅仅是结构。
template<class T, auto Tag>
struct strong:T{
using T::T;
};
现在标签是任何编译时值。
using StringEventA=strong<std::string, 'A'>;
using StringEventB=strong<std::stings, 'B'>;
比如char
文字。
您的编译器可能不支持auto
模板参数。查看。
推荐阅读
- azure - Azure WebApp-WebAPI->授权类型授权代码不返回 RefreshToken
- python - 基于python-docx,在特定文本python之后创建表
- java - Sublime Text 侧边栏突出显示更改的行
- php - 通过 phpspreadsheet 在网站上上传 Excel 数据
- database - 如何使用数据库,使用 Hyperledger Fabric CA 对多个用户进行 jwt 身份验证?
- rows - codeigniter v4 num_rows() 调用未定义的方法
- r - 创建观察的相对位置
- python - 当 Python 中有多个相同的值时,如何找到特定元素的索引?
- javascript - 从右到左绘制水平条形图
- angular - 使用pimeng图表设置最小值yAxes