c++ - 它们的表示是它们与 C++ 具体类型相关的定义的一部分?

标签 c++ c++11

在他的两本书中

C++ 编程语言,2013 年(第 4 版)和 C++ 之旅,2013 年

Bjarne Stroustrup 写道:

Types such as complex ... are called concrete types because their representation is part of their definition.

以下内容在一定程度上澄清了上述说法:

In that, they resemble built-in types. In contrast, an abstract type is a type that completely insulates a user from implementation details. To do that, we decouple the interface from the representation and give up genuine local variables. Since we don’t know anything about the representation of an abstract type (not even its size), we must allocate objects on the free store and access them through references or pointers.


问题

在短语“...他们的表示是他们定义的一部分。”

  1. 类型表示是什么意思?也就是说,究竟表示什么:对象在内存中的布局?该类型持有的私有(private)和公共(public)数据?还是别的?

  2. 类型定义是什么意思?

  3. 类型表示和定义的这些典型含义是否与 C++ 相关?


我决定做更多的研究,并检查了其他来源。首先,我查看了说明 C++ 编程语言实现要求的 ISO/IEC 14882:2011 规范,然后查看了其他来源。

广告问题 1

我无法在 ISO 规范中找到任何类似“类型表示”或“类型表示”的内容。相反,有 2 个与对象相关的术语:

The object representation of an object of type T is the sequence of N unsigned char objects taken up by the object of type T, where N equals sizeof(T).

The value representation of an object is the set of bits that hold the value of type T. For trivially copyable types, the value representation is a set of bits in the object representation that determines a value, which is one discrete element of an implementation-defined set of values.

所以在我看来,术语类型表示在 ISO 标准中没有任何常规含义。

好的。也许这是 ISO 标准之外的东西?让我们看看是什么 Linux Standard Base C++ Specification 3.1 > Chapter 7. C++ Class Representations > 7.1. C++ Data Representation说:

An object file generated by the compilation process for a C++ program shall contain several closely related internal objects, or Class Components, to represent each C++ Class. Such objects are not a visible part of the source code. The following table describes these Class Components at a high level.

Table Class Components

Object.......................Contains
=----------------------------------------=
Class Data...................Class members
Virtual Table................Information needed to dispatch virtual functions,
                             access virtual base class subobjects and to access
                             the RTTI information
RTTI.........................Run-Time Type Information used by the typeid and
                             dynamic_cast operators, and exception handlers
Typeinfo Name................String representation of Class name
Construction Virtual Table...Information needed during construction and
                             destruction of Classes with non-trivial
                             inheritance relationships.
VTT..........................A table of virtual table pointers which holds the
                             addresses of construction and non-construction
                             virtual tables.

广告问题 2

我再次无法在 ISO 规范中找到对类型定义的明确解释。

相反,我发现了以下内容:

A declaration may introduce one or more names into a translation unit... A class declaration introduces the class name into the scope where it is declared...A declaration is a definition unless [I removed things not directly related to the class declaration], ... it is a class name declaration...

这是 Microsoft 对同一事物的解释:

C++ Declarations - MSDN - Microsoft

A declaration introduces one or more names into a program. Declarations can occur more than once in a program...Declarations also serve as definitions, except when the declaration:...;Is a class name declaration with no following definition, such as class T;...

C++ Definitions - MSDN - Microsoft

A definition is a unique specification of an object or variable, function, class, or enumerator. Because definitions must be unique, a program can contain only one definition for a given program element. There can be a many-to-one correspondence between declarations and definitions. There are two cases in which a program element can be declared and not defined: A function is declared but never referenced with a function call or with an expression that takes the function's address. A class is used only in a way that does not require its definition be known.

例子:

struct S;    // declares, but not defines S
class T {};  // declares, and defines T
class P { int a;};  // declares, and defines P, P::a

结论:

候选答案N1:
由 Jonathan Wakely 提议
(以下是我的理解)
短语“类型如复杂......被称为具体类型,因为它们的表示是它们定义的一部分”应该按以下方式解释和理解:
their(=type) definition是一个c++技术术语,其含义是约定俗成的,可以在c++规范中找到;
their(=type) representation(根据 Jonathan Wakely 的说法)在这种情况下不是一个技术性的 c++ 术语,但任何懂英语的人都可以很容易地理解它的含义(并且可能,这是我的猜测,之前已经接触过大量的 C++ 代码和文本)。这种情况下的类型表示意味着 “定义类型及其作用的属性”,即:
“对于具体类型:其成员的类型和布局”,
“对于抽象类型:其成员函数及其可观察的行为”
● 整个短语 then(我们谈论的是具体类)翻译为:
类型,例如复杂的......被称为具体类型,因为其成员的类型和布局是其定义的一部分
我认为这种解释是有道理的,可以理解的,并且与BS书籍中的解释也很吻合。

如果这里有什么不对请指正**

最佳答案

QUESTIONS: in the phrase "...their representation is part of their definition." 1) What is the meaning of type representation? (that is, the representation of WHAT exactly: object layout in memory or private and public data that the type holds OR something else) 2) What is the meaning of type definition? 3) Are these typical meanings of type representation and definition as related to c++?

您要询问 Stroustrup 在您引用的文本中没有使用的术语的含义!

他并没有像 C++ 标准那样尝试定义诸如“类型表示”之类的术语的正式规范,他正在撰写更为非正式的散文。您找到的所有对技术术语的引用都是误导性的,并且不直接相关。

(that is, the representation of WHAT exactly: object layout in memory or private and public data that the type holds OR something else)

是的,你提到的这两件事。对于具体类型,定义它是什么以及它做什么的属性包括其成员的类型和布局。即它在源代码中的表示方式。

对于一个抽象类,定义它是什么和它做什么的属性是它的成员函数和它们的可观察行为。它如何产生可观察行为的细节不一定重要,有时甚至在源代码中不可见,因为您实际上使用了在另一段代码中定义的一些具体类,并且仅通过抽象接口(interface)使用它。

编辑:从您在下面写的评论来看,您显然错过了我试图给您答案的内容。我上面写的是指定义什么是类型及其作用的属性。那是“类型的定义”。

如果您必须为用户编写 C++ 类型的文档,您将如何定义它?

对于具体类型,您可以描述其成员的类型,从而根据其成员的属性定义其某些属性。例如“一个 std::complex<float> 存储两个 float 成员,代表复数的实部和虚部。”这告诉你 std::complex<float>只能存储与 float 精度相同的复数,即它的精度取决于它用两个 float 表示的事实成员。

对于抽象类,您将描述其成员函数的行为,这可能是 virtual ,因此您根据其遵循的接口(interface)而不是其实现的细节来描述它。

但它们不是正式术语,我认为您将它们视为严格的技术术语是错误的。他只是在使用具有通常英语含义的单词。

关于c++ - 它们的表示是它们与 C++ 具体类型相关的定义的一部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25853450/

相关文章:

c++ - 在下面的代码中,如何在不使用虚拟值的情况下初始化类成员?

c++ - 以特权用户身份运行 Windows 服务

c++ - 如何用 C++ 数组填充 torch::tensor?

c++ - 如何使用 C++11 std::thread 设置堆栈大小

gcc - 使用 c++0x 在 g++ 上出现 strdup 错误

c++ - 在 C++11 中将静态 constexpr 数组转换为模板参数

c++ - 有符号和无符号整数表达式与 0x80000000 之间的比较

c++ - 为什么这个段错误没有

c++ - 什么是 "template<class T> using owner = T;"?

c++ - 构造函数中没有匹配的调用函数 - C++ 11