首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在C++11中定义强ID类型?

如何在C++11中定义强ID类型?
EN

Stack Overflow用户
提问于 2013-08-20 14:52:16
回答 1查看 459关注 0票数 6

如何在C++11中定义强ID类型?可以使用整数类型的别名,但是在混合类型时会收到编译器的警告?

例如:

代码语言:javascript
复制
using monsterID = int;
using weaponID = int;

auto dragon = monsterID{1};
auto sword = weaponID{1};

dragon = sword; // I want a compiler warning here!!

if( dragon == sword ){ // also I want a compiler warning here!!
    // you should not mix weapons with monsters!!!
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-08-20 14:56:28

如果您正在使用boost,请尝试蒂佩德夫

文档中的示例:

代码语言:javascript
复制
BOOST_STRONG_TYPEDEF(int, a)
void f(int x);  // (1) function to handle simple integers
void f(a x);    // (2) special function to handle integers of type a 
int main(){
    int x = 1;
    a y;
    y = x;      // other operations permitted as a is converted as necessary
    f(x);       // chooses (1)
    f(y);       // chooses (2)
}
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18338228

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档