与C ++中的Ada子类型等效 [英] Ada subtype equivalent in C++

查看:130
本文介绍了与C ++中的Ada子类型等效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++是否提供类似于Ada的子类型的东西来缩小类型?

Does C++ offer something similar to Ada's subtype to narrow a type?

例如:

type Weekday is (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday);
subtype Working_Day is Weekday range Monday .. Friday;


推荐答案

不,不是本地的。

您所描述的内容最好用范围枚举表示,并带有一个单独的范围枚举和一个子集,这些枚举与父范围枚举共享数值表示。

What you describe might be best represented as a scoped enum, accompanied by a separate scoped enum with a subset of enumerations who share numerical representations with the "parent" scoped enum.

您可以进一步在两者之间定义一些转换,但是如果没有反思,就不可能使所有内容都优雅而直观,至少没有硬编码和重复大量工作会破坏目的。

You could further define some conversions between the two, but without reflection it's not really possible to make it all elegant and intuitive, at least not without hardcoding and duplicating loads of stuff which rather defeats the purpose.

最好是在进行C ++编程时,完全放弃使用其他语言进行编程所产生的思维方式。

It would be best, when programming C++, to attempt entirely abandoning the mindset imbued by programming in other languages.

话虽如此,但这实际上是一个很好的功能构想,尽管我不会屏住呼吸!

That being said, this is actually quite a nice feature idea, though I wouldn't hold my breath!

解决方法:只使用一个枚举,并应用范围检查您需要的地方。

Workaround: just use an enum, and apply range checking where you need to.

这篇关于与C ++中的Ada子类型等效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆