什么是标准C的约束? [英] What are the Constraints in Standard C?

查看:112
本文介绍了什么是标准C的约束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C类标准谈的限制的,E。 G。 ISO / IEC 9899:201X 定义的术语


  

约束结果
  限制,无论是语法或语义,通过该
  语言要素的论述,是要跨preTED


和章说的一致性


  

如果出现的外''应当''或''不得'的要求
  约束或运行时违反约束,行为
  不确定的。


在本章的环保,分段的诊断据说


  

一个符合标准的实现必须产生至少一种诊断
  消息(以实现定义的方式确定),如果
  preprocessing翻译单元或翻译单元包含一个
  违反任何语法规则或约束,即使行为
  还明确规定为未定义或实现定义的。


所以,重要的是要知道什么是C中的约束,例如当诊断,而不是仅仅未定义行为可以预期当需要诊断编译器的编写者来判断,或者C程序员。结果
现在,有部分遍的标题限制标准的文档,但我找不到确切的字眼,究竟这个术语的约束的覆盖标准。


  • 是出现在标题为部分约束一切的限制

  • 是?
  • 即那些部分的约束之外没有说明每一个要求
  • 是否有一个COM prehensive描述的约束的在,我错过了标准?


解决方案

  

是出现在标题为约束部分约束的一切吗?


在n1570 3.8(征收程序的限制,这需要一个一致的实现发出编译时侵犯时诊断消息),我想是的。感。


  

是那些部分以外指出每一个要求没有约束?


在3.8感,我想是的,但更圆的原因:标准的结构是相当正式。每当适用似乎有一个明确的限制的部分。因此,我理解的定义的东西是不是在一个的限制的部分是不是在3.8意义上的约束。结果
有外一些应当条款的限制的这完全显示部分编译时强制执行的,比照。下面的几个例子。他们在相邻的语义的部分往往。我可能缺少其中prevent编译时检测在一般情况下(以使诊断不能进行能强制的)微妙之处,或者标准是不完全一致。但我认为,一个编译器可以简单地翻译了违反程序,准确的,因为的要求不是在限制的部分。


  

有一个COM prehensive描述的约束的在,我错过了标准?


我觉得3.8是你所得到的。我尝试探索以下期限和同意的定义是不令人满意的。


我看着更深的标准找到了。这是我的研究工作。

术语的约束

让我们从基础开始。在其中引用3.8约束的定义是令人惊讶的很难理解,至少在没有上下文(限制,无论是语法和语义,由语言要素的论述是要PTED间$ P $)。 限购和约束是同义词,使措辞不会​​增加太多;什么是语言元素的阐述是什么意思?博览会是几个含义的词;让我们的书面或口头主要是为了传递信息来自 Dictionary.com 的一>,并假设他们的意思与标准。那么就意味着基本上是一个的约束的本标准是在这个标准说的约束。哇,我也不会猜到。

限制的按3.8

务实刚刚检查实际的限制的标准部分显示,他们列出的编译强加给符合的节目时间的限制这是有道理的,因为只有编译时间限制可以在编译时进行检查。
这些额外的限制是那些无法在C语法pssed前$ P $ 1

之外的约束的限制的部分

应当的的限制的部分的强加一个​​一致的实施的限制外示例的大多数用途:所有对象与静态存储时间应被初始化(设置为其
前程序启动初始值),
的符合规范的实施工作。

有一些应当条款强加外部上的程序的(不执行)的限制的限制的虽然部分。我认为大部分落在同一类别的运行时的限制[...]上的程序调用库函数时中提到的3.18。他们似乎要运行哪些不是在编译时(因此诊断不能强制)。

一般检测时间限制

下面是几个例子。

在6.5 / 7 n1570详细介绍了倍受争议的别名规则:


  

对象应具有其存储的值只访问
  由具有之一的左值前pression
  以下类型:


  
  

      
  • 一个类型的有效对象的类型
  • 兼容
      
  • 兼容的类型的合格版本
      用有效的对象的类型,
      [...]

  •   

在6.5.16.1,简单分配:


  

如果该值被存储在对象是从以任何方式重叠另一个目的读
  所述第一对象的存储,则该重叠应确切[..]。


其他的例子关注指针运算(6.5.6 / 8)。

谈情的条款可能是在限制的部分

但后来有其他条款不得违反其应该是在编译时检测;我也不会眨了眼睛,如果他们出现在各自的限制的部分。


  • 6.6 / 6的铸运营商在一个整型常量
    前pression只能转换算术类型为整数类型
    的(在语义);你能发现在编译的时候,如果你无法检测类型的常量和管型

  • 6.7 / 7的如果一个对象的标识符与无连锁宣布,该对象的类型应在其声明的年底完成的(在语义)。对我来说似乎是一个基本的编译器的任务来检测类型是否是在code某些时候完成。但当然,我从来没有写过一个C编译器。

有几个例子。但正如我所说,我认为实现不需要诊断违法行为。它管理潜行过去的编译器违反程序只是简单地暴露未定义的行为。



<子> 1 例如,据我所知,语法不对付的类型 - 它只有通用的前pressions。因此,每个运营商都有的限制的节详述允许的类型它的参数。举例移位运算符:每个操作数应具有整型。的这竟将一个浮动的比特违反了这个限制,并且该实现必须发出诊断程序

C standards talk about constraints, e. g. ISO/IEC 9899:201x defines the term

constraint
restriction, either syntactic or semantic, by which the exposition of language elements is to be interpreted

and says in chapter Conformance

If a ‘‘shall’’ or ‘‘shall not’’ requirement that appears outside of a constraint or runtime-constraint is violated, the behavior is undefined.

In chapter Environment, Subsection Diagnostics it is said

A conforming implementation shall produce at least one diagnostic message (identified in an implementation-defined manner) if a preprocessing translation unit or translation unit contains a violation of any syntax rule or constraint, even if the behavior is also explicitly specified as undefined or implementation-defined.

So, it is important to know what are the constraints in C, for example for compiler writers to judge when diagnostics are required, or for C programmers when diagnostics rather than just undefined behaviour can be expected.
Now, there are sections all over the standard document with the title Constraints, but I cannot find definitive wording as to what exactly the term constraint covers in the standard.

  • Are the constraints everything that appears in the sections titled Constraints?
  • Is every requirement that is stated outside of those sections not a constraint?
  • Is there a comprehensive description of constraint in the standard that I missed?

解决方案

Are the constraints everything that appears in the sections titled Constraints?

In the sense of n1570 3.8 (a restriction imposed on programs which requires a conforming implementation to issue a compile-time diagnostic message when violated), I think yes.

Is every requirement that is stated outside of those sections not a constraint?

In the sense of 3.8, I think yes, but for a more circular reason: The standard's structure is fairly formal. Whenever applicable there seems to be an explicit Constraints section. Therefore I understand that by definition anything which is not in a Constraints section is not a constraint in the sense of 3.8.
There are a few "shall" clauses outside Constraints sections which appear completely compile-time enforceable, cf. below for a few examples. They are often in adjacent Semantics sections. I may be missing subtleties which prevent compile-time detection in the general case (so that a diagnosis cannot be made mandatory), or perhaps the standard is not completely consistent. But I would think that a compiler could simply translate a violating program, exactly because the requirements are not in a Constraints section.

Is there a comprehensive description of constraint in the standard that I missed?

I think 3.8 is all you get. I try to explore the term below and agree that the definition is unsatisfying.


I looked deeper into the standard to find that out. Here is my research.

The term constraint

Let's start with the basics. The definition of "constraint" in 3.8 which you quote is surprisingly hard to understand, at least without context ("restriction, either syntactic or semantic, by which the exposition of language elements is to be interpreted"). "Restriction" and "constraint" are synonyms, so that the rewording doesn't add much; and what is meant by "exposition of language elements"?? Exposition is a word with several meanings; let's take "writing or speech primarily intended to convey information" from Dictionary.com, and let's assume they mean the standard with that. Then it means basically that a constraint in this standard is a constraint of what is said in this standard. Wow, I wouldn't have guessed that.

Constraints as per 3.8

Pragmatically just examining the actual Constraints sections in the standard shows that they list compile time restrictions imposed on conforming programs. This makes sense because only compile-time constraints can be checked at compile time. These additional restrictions are those which cannot be expressed in the C syntax.1

Constraints outside Constraints sections

Most uses of "shall" outside of Constraints sections impose restrictions on a conforming implementation. Example: "All objects with static storage duration shall be initialized (set to their initial values) before program startup", a job of a conforming implementation.

There are a few "shall" clauses imposing restrictions on a program (not the implementation) outside of Constraints sections though. I would argue that most fall in the same category as the "runtime constraints [...] on a program when calling a library function" mentioned in 3.18. They seem to be run time constraints which are not generally detectable at compile time (so that diagnostics can not be mandatory).

Here are a few examples.

In 6.5/7 n1570 details the much-debated aliasing rules:

An object shall have its stored value accessed only by an lvalue expression that has one of the following types:

  • a type compatible with the effective type of the object
  • a qualified version of a type compatible with the effective type of the object, [...]

In 6.5.16.1, "Simple Assignment":

If the value being stored in an object is read from another object that overlaps in any way the storage of the first object, then the overlap shall be exact[..]."

Other examples concern pointer arithmetic (6.5.6/8).

Shall clauses which could be in Constraints sections

But then there are other shall clauses whose violation should be detectable at compile time; I would not have blinked if they had appeared in the respective Constraints section.

  • 6.6/6, "Cast operators in an integer constant expression shall only convert arithmetic types to integer types" (under "Semantics"); what can you detect at compile time if you cannot detect types of constants and casts?
  • 6.7/7, "If an identifier for an object is declared with no linkage, the type for the object shall be complete by the end of its declarator" (under "Semantics"). To me is seems to be a basic compiler task to detect whether a type is complete at some point in the code. But of course, I have never written a C compiler.

There are a few more examples. But as I said, I would think that an implementation is not required to diagnose violations. A violating program which manages to sneak past the compiler simply exposes undefined behavior.


1 For example, I understand that the syntax doesn't deal with types -- it only has generic "expressions". Therefore every operator has a Constraints section detailing the permissible types of its arguments. Example for shift operators: "Each of the operands shall have integer type." A program which is trying to shift the bits of a float is violating this constraint, and the implementation must issue a diagnostic.

这篇关于什么是标准C的约束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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