首页 > 解决方案 > C++ 为什么 typedef 结构与 typedef 原语相比是独一无二的

问题描述

为什么 a typedeffor astruct被认为是唯一类型,而 a typedeffor astd::string或其他原语则不是?考虑到您可能有两个structs 具有相同的组件,但它被视为两种不同的、不相关的类型。

这是一个工作示例。

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

标签: c++

解决方案


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模板参数。查看。


推荐阅读