typedefs和隐式强制转换

Typdefs and implicit cast

本文关键字:转换 typedefs      更新时间:2023-10-16

示例:

int main() 
{
  typedef int Oranges;
  typedef int Apples;
 /* ... a lot of other code */
  Oranges not_apples = 10;
  Apples apples = not_apples; // ??? confusing
}

问题:对于通过typedefs声明的变量,我们可以禁止隐式强制转换吗?

typedef实际上只为基类型创建了一个别名,所以您创建的两个类型实际上都只是int的别名。你要找的是boost::strong_typedef

您可能想要在结构(类)内部隐藏typedef’ed类型。无论如何,几乎所有的抽象值迟早都需要一个比简单的"int"更详细的表示。