简体   繁体   English

C 中的 Ada 类型声明

[英]Ada type declaration in C

Is there a way to declare a new integer type in C?有没有办法在 C 中声明一个新的整数类型?

In Ada,在阿达,

type a is new integer;

means that a is not equivalent to say, an integer variable x.意味着 a 不等同于说,一个整数变量 x。

I know that typedef can create an alias of an integer, but it is not the same as the above Ada code.我知道typedef可以创建整数的别名,但是和上面的Ada代码不一样。 It is more like a subtype in Ada.它更像是 Ada 中的一个子类型。

Does anyone know?有人知道吗?

The type systems in C and Ada are different. C 和 Ada 中的类型系统是不同的。

C uses a structural typing system for primitive types and a nominative typing system for structs and unions. C 对原始类型使用结构类型系统,对结构和联合使用主格类型系统。 C enums are simply named integer values, not a separate type from int. C 枚举只是命名的整数值,而不是独立于 int 的类型。 Since all int instances in C have the same structural representation on a given hardware platform, all typedefs of int are the same type.由于 C 中的所有 int 实例在给定的硬件平台上都具有相同的结构表示,因此 int 的所有 typedef 都是相同的类型。

Ada uses a nominative typing system for all types. Ada 对所有类型使用主格类型系统。 Types are distinguished by their name and not by their structural layout.类型是通过它们的名称而不是它们的结构布局来区分的。 Thus type Integer is different than因此类型 Integer 不同于

type My_Int is new Integer;

My_Int is a type with a different name than Integer, even though it is derived from Integer. My_Int 是一种与 Integer 名称不同的类型,即使它是从 Integer 派生的。 Therefore My_Int is not an Integer in Ada even though the two types share the same operations and the same memory layout.因此 My_Int 不是 Ada 中的整数,即使这两种类型共享相同的操作和相同的内存布局。

AC typedef is similar to an Ada subtype inasmuch as a subtype is a member of its parent type. AC typedef 类似于 Ada 子类型,因为子类型是其父类型的成员。 Subtypes and typedefs differ because a subtype of a scalar type may be defined with a restricted range such as:子类型和 typedef 不同,因为标量类型的子类型可能定义为有限范围,例如:

subtype Natural is Integer range 0..Integer'Last;
subtype Positive is Integer range 1..Integer'Last;

Every instance of Natural or Positive is an Integer value. Natural 或 Positive 的每个实例都是一个整数值。 The subtypes have a restricted range of valid values compared to their base type.与其基类型相比,子类型的有效值范围有限。

There is no simple way a typedef statement in C can declare a restricted range of values. C 中的 typedef 语句没有简单的方法可以声明有限范围的值。 Proposals to that effect in the past have been rejected by the C standard committee. C 标准委员会过去曾否决过这种效果的提案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM