c++ - Typdef 和隐式转换

标签 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 声明的变量进行隐式转换?

最佳答案

typedef 实际上只是创建了一个基本类型的别名,因此您创建的两种类型实际上只是 int 的别名。您正在寻找的是 boost::strong_typedef

关于c++ - Typdef 和隐式转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9510185/

相关文章:

ios - xcode 未获取头文件中的 typedef

c++ - Qt Widget - 如何只捕获几个键盘键

c++ - SFML 2.1 获取帧时间

c - 为什么结构名称应该有 typedef?

c - 使用带有指针的 typedef const 结构

c - strcpy 通过传递给函数的字符串数组?

c++ - 这个循环是否隐式地将一个 int 转换为 size_t?

c++ - std::literals::.. 作为内联命名空间有什么好处?

c++ - 函数模板中的参数推导

c# - 无法将 Type<Food> 隐式转换为 My Type<IFood>