在他的两本书中
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.
在短语“...他们的表示是他们定义的一部分。”
类型表示是什么意思?也就是说,究竟表示什么:对象在内存中的布局?该类型持有的私有(private)和公共(public)数据?还是别的?
类型定义是什么意思?
类型表示和定义的这些典型含义是否与 C++ 相关?
我决定做更多的研究,并检查了其他来源。首先,我查看了说明 C++ 编程语言实现要求的 ISO/IEC 14882:2011 规范,然后查看了其他来源。
我无法在 ISO 规范中找到任何类似“类型表示”或“类型表示”的内容。相反,有 2 个与对象相关的术语:
The object representation of an object of type
Tis the sequence of Nunsigned charobjects taken up by the object of typeT, where N equalssizeof(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.
我再次无法在 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/
我正在尝试设置一个puppet节点,但rubygems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由rubygems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢
我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby1.9+ 关于ruby-主要:Objectwhenrun
我可以得到Infinity和NaNn=9.0/0#=>Infinityn.class#=>Floatm=0/0.0#=>NaNm.class#=>Float但是当我想直接访问Infinity或NaN时:Infinity#=>uninitializedconstantInfinity(NameError)NaN#=>uninitializedconstantNaN(NameError)什么是Infinity和NaN?它们是对象、关键字还是其他东西? 最佳答案 您看到打印为Infinity和NaN的只是Float类的两个特殊实例的字符串
我不确定传递给方法的对象的类型是否正确。我可能会将一个字符串传递给一个只能处理整数的函数。某种运行时保证怎么样?我看不到比以下更好的选择:defsomeFixNumMangler(input)raise"wrongtype:integerrequired"unlessinput.class==FixNumother_stuffend有更好的选择吗? 最佳答案 使用Kernel#Integer在使用之前转换输入的方法。当无法以任何合理的方式将输入转换为整数时,它将引发ArgumentError。defmy_method(number)
我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que
我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin