Typdefs和隐式演员

时间:2012-03-01 03:22:10

标签: c++ typedef implicit-conversion

示例:

int main() 
{
  typedef int Oranges;
  typedef int Apples;

 /* ... a lot of other code */

  Oranges not_apples = 10;
  Apples apples = not_apples; // ??? confusing
}

问题:我们是否可以禁止对通过typedef声明的变量进行隐式转换?

2 个答案:

答案 0 :(得分:3)

typedef实际上只创建了基类型的别名,因此您创建的两种类型实际上只是int的别名。你要找的是boost::strong_typedef

答案 1 :(得分:1)

您可能想要在结构(类)中隐藏typedef类型。无论如何,几乎所有的抽象值都需要比简单的“int”更详细的重复,迟早。

相关问题