Mr Flibble <
flibbleREM...@i42.co.uk> writes:
> On 22/06/2017 14:52, Tim Rentsch wrote:
>
>> Another way of expressing the second point is not being able to
>> distinguish types and classes. It's been known for more than 30
>> years that types and classes are not synonymous, but that
>> understanding hasn't yet percolated out to the community at
>> large. Unfortunately C++ is firmly wedded to the concept of
>> seeing classes as types (or sometimes the other way around)
>> rather than supporting the two as distinct notions. Given how
>> C++ has evolved it would be very difficult to change that now,
>> but I think it's worth starting the conversation, so that at
>> least the understanding will be there even if it takes the
>> language a while to catch up.
>
> What absolutely nonsense! Of course classes are types. An instance of
> a type is an object, thus:
>
> int a = 42; // a is the name of an OBJECT of scalar TYPE 'int'.
> foo o = 42; // o is the name of an OBJECT of class TYPE 'foo'.
First let me clear up a confusion. My earlier comment was made
in a limited context, where the only kind of data structuring
being considered is classes. Of course there are other kinds
of data structuring constructs available, eg, built-in ones like
'int' or 'double', or user-defined ones like enumerations. Let's
use the word "structure" to encompass all of the different sorts
of things that define the layout or memory structure of objects.
So "structure" includes int, double, structs, unions, classes,
pointers, arrays, etc.
Returning to the main point under discussion, what I said is
classes (or more generally, structures) are not /synonymous/ with
types. A class (or other kind of structuring construct) can
stand in for a type, but it is not the same as a type. A type
can be more information than a structure, eg,
int pi; // "plain" int
const int ci;
volatile int vi;
The variables pi, ci, and vi all have the same structure, namely
the same size, alignment, and representation that any 'int'
object has. But they have different types, which means different
rules for how they can be used. For example, it's okay to access
the variable 'ci' as if it were just a plain int, as long as the
access is a read access, not a write access. The variable 'vi'
can be accessed as if it were a const volatile int, but not as
if it were a const int.
A type can be less information than a structure, as can be seen
with array declarations:
extern Animal *zoo[];
Here we know 'zoo' is an array (of pointers to Animal, which need
not concern us further), but we don't know how big the array is.
The type tells us /partial/ information about the structure, but
not complete information.
Some types don't deal with structure at all:
extern void initialize_recovery_system( int threads );
Here of course we have a function type. There is no structure
information associated with a function type.
In C++, reference types seem to fall in a strange kind of limbo.
Of course what we expect is that references will be implemented
by holding on to some kind of pointer, but the C++ Standard
insists that references are not objects (or maybe that they
might no be objects? I'm not sure). If T is a reference
type, then 'new T' doesn't work. But it's okay for a class or
struct to have a member of a reference type, and you can bet
your bottom dollar that how the class/struct is laid out will
have some space allocated for that reference member.
If anyone is interested to look into this further I recommend
this paper, which AFAIK is the earliest explanation for how types
and structures are different. (In Smalltalk the only kind of
structuring construct is classes, but generalizing to other
kinds of structuring mechanisms should be easy to see.)
A type declaration and inference system for Smalltalk (1982)
(
http://citeseer.ist.psu.edu/showciting?cid=104638)
Roughly speaking, a "type" is like an interface, and a "structure"
is like an implementation. But don't take that literally, it is
meant just as a way to aid understanding, not as an exact analogy.