Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

their representation is part of their definition as related to C++ concrete types?

Tags:

c++

c++11

In both of his books

The C++ Programming Language, 2013 (4th edition) and A Tour of C++, 2013

Bjarne Stroustrup writes:

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

What follows to some extent clarifies the above statement:

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.


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: The object layout in memory? The 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++?


I decided to do some more research and I checked other sources. First I looked through ISO/IEC 14882:2011 specifications that state requirements for implementations of the C++ programming language, then through other sources.

Ad question 1

I was not able to find in ISO specs anything like "type representation" or "representation of a type". Instead there are 2 terms related to objects:

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.

So it seems to me that the term type representation does not have any conventional meaning within the ISO standards.

Ok. Maybe it is something outside the ISO standards? Let's see what Linux Standard Base C++ Specification 3.1 > Chapter 7. C++ Class Representations > 7.1. C++ Data Representation says:

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.

Ad question 2

I was again not able to find in ISO specs an explicit explanation of type definition.

Instead I found the following:

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...

Here is a Microsoft interpretation of the same thing:

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;...

and

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.

Examples:

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

Conclusions:

Candidate Answer N1:
proposed by Jonathan Wakely
(below is my understanding)
The phrase "Types such as complex ... are called concrete types because their representation is part of their definition" should be interpreted and understood in the following way:
their(=type) definition is a technical c++ term whose meaning is conventional and can be found in c++ specs;
their(=type) representation is (according to Jonathan Wakely) not a technical c++ term in this context, but its meaning can be easily figured out by anybody who understands English language well enough (and probably, it is my guess, has been previously exposed to the generous amount of c++ codes and texts). Type representation in this context means "the properties that define what the type is and what it does", that is:
"for a concrete type: the type and layout of its members",
"for an abstract type: its member functions and their observable behavior"
● The whole phrase then (we are talking about the concrete classes) translates to:
"Types such as complex ... are called concrete types because the types and layouts of their members are part of their definition"
I think this interpretation makes sense, is understandable, and also agrees well with what follows it in the BS books.

Please correct me if something here is not ok**

like image 467
yus Avatar asked Sep 15 '14 17:09

yus


2 Answers

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++?

You're asking for the meaning of terms that Stroustrup doesn't use in the text you quoted!

He's not trying to define a formal specification of a term like "type representation" the way the C++ standard does, he's writing prose that is more informal. All the references to technical terms that you've dug up are misleading and not directly relevant.

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

Yes, both the things you mention. For a concrete type the properties that define what it is and what it does include the type and layout of its members. i.e. how it is represented in the source code.

For an abstract class, the properties that define what it is and what it does are its member functions and their observable behaviour. The details of how it produces that observable behaviour are not necessarily important, and sometimes aren't even visible in the source code because you actually use some concrete class defined in another piece of code and only use it through an abstract interface.

Edit: Judging from the comments you wrote below you apparently missed that I tried to give you an answer. What I wrote above refers to the properties that define what a type is and what it does. That is a "definition of a type".

If you had to write documentation for a C++ type for users, how would you define it?

For a concrete type you might describe the types of its members and so define some of its properties in terms of the properties of its members. e.g. "A std::complex<float> stores two float members, which represent the real and imaginary parts of the complex number." This tells you that std::complex<float> can only store complex numbers with the same precision as float, i.e. its precision is determined by the fact it is represented using two float members.

For an abstract class you would describe the behaviour of its member functions, which are likely to be virtual, so you describe it in terms of the interface it follows, not in terms of the details of its implementation.

But they are not formal terms, I think you are wrong to treat them as strict technical terms. He's just using the words with their usual English meaning.

like image 137
Jonathan Wakely Avatar answered Sep 23 '22 13:09

Jonathan Wakely


You go looking out for a vegetable in dinner tonight. Wait.. a vegetable? The word vegetable defines something for sure but it carries no representation. Someone will surely ask you which vegetable. So a vegetable is an abstract concept.

So now you order some potatoes and onions. Well, they define some properties and represent themselves well enough so that you can locate them in the store. Potatoes and onions make up for concrete representation of a type with a well defined property and behavior.

Try writing two classes following this analogy. You may connect to what is meant by representation is part of their definition.

like image 22
nightlytrails Avatar answered Sep 26 '22 13:09

nightlytrails