【问题标题】:Does the C++ standard require a binary machine?C++ 标准是否需要二进制机器?
【发布时间】:2012-10-10 15:29:44
【问题描述】:

我在标准中找到:

整数类型的表示应使用纯二进制计数系统定义值

但我的理解是,这并不是 C++ 必须在基于硬件基础 2 的机器上实现的要求;换句话说,这似乎只是程序员应该看到的要求。

C++ 标准是否需要二进制 CPU,或者是否可以在三进制或十进制机器上实现有效的 C++ 实现?


微小的背景信息:在历史上,已经有使用小数、三进制或 C++ 字节长度为 36 位的计算机 (PDP-10)

【问题讨论】:

  • 很难看出例如按位运算符可以在非二进制架构上工作,除非您在两者之间有某种二进制虚拟机,在这种情况下,底层架构没有实际意义。
  • 标准中的那一段对我来说似乎很清楚。它需要一台能够对数字进行二进制表示的机器。换句话说,一台典型的现代数字计算机,或者至少是一台可以模拟二进制数的计算机。
  • N3337 3.9.1 Fundamental types Types bool, char, char16_t, char32_t, wchar_t, and the signed and unsigned integer types are collectively called integral types. A synonym for integral type is integer type. The representations of integral types shall define values by use of a pure binary numeration system. [ Example: this International Standard permits 2's complement, 1's complement and signed magnitude representations for integral types. —end example ] 我想这可能属于“好像”。
  • 还有N3337 4.7 Integral conversions If the destination type is unsigned, the resulting value is the least unsigned integer congruent to the source integer (modulo 2^n where n is the number of bits used to represent the unsigned type). [ Note: In a two's complement representation, this conversion is conceptual and there is no change in the bit pattern (if there is no truncation). —end note ]

标签: c++ language-lawyer


【解决方案1】:

虽然从未在标准中直接说明,但有一条“好像”规则适用于 C++(和 C)标准中的所有要求。

换句话说,你可以打破任何你想打破的规则,只要你产生与你遵守规定的规则相同的外部可观察效果。尽管标准本身没有说明,但这条规则在 C 标准的Rationale 中明确说明。据我所知,C++ 标准没有匹配的基本原理,但两者的关系非常密切,我认为将规则应用于两者几乎没有问题。

【讨论】:

  • 我相信有一个脚注提到了 1.9.1 下的“as-if”。
  • 我不知道早期的标准,但在 C++11 § 1.9/1 中直接说明了这一点:The semantic descriptions in this International Standard define a parameterized nondeterministic abstract machine. This International Standard places no requirement on the structure of conforming implementations. In particular, they need not copy or emulate the structure of the abstract machine. Rather, conforming implementations are required to emulate (only) the observable behavior of the abstract machine as explained below.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-28
  • 2016-12-10
相关资源
最近更新 更多