Search code examples
c++typesqualifiers

Defining a multi word dataype


C++ has the build-in types such as long long int, unsigend int. Type can be combined with qualifiers, such as const, volatile and mutable.

Why is long long int a datatype? Why is it not named longLongInt? How does a multi word datatype work? Can I define my own?

Is it possible to define a customized qualifier with respect to a custom datatype?

For example consider a linear algebra vector in 3D. One could define a class LinAlVector, which contains the x-,y-,z-components of the vector.

If I now need to ensure, that this vector is a unit vector (length is equal to 1), I am wondering if I can write

isUnit LinAlVector vec(x,y,z);

where isUnit is a modifier which is effecting the behaviour of LinAlVector (e.g. compiler error if a non unit vector is defined).

I know that the OO straight forward way would be to derive a class UnitLinAlVector from LinAlVector.


Solution

  • Why is long long int a datatype? Why is it not named longLongInt?

    Because that is the name that was decided by the standard committee. The name was probably chosen because that's the same name as the equivalent data type in C99.

    For what it's worth, if it were named longLongInt, then a new keyword would have had to be introduced into the language. That is something to be avoided because introducing a new keyword is not backwards compatible. long long int did not introduce any keywords because long and int were already keywords.

    Can I define my own?

    No, as a user of the language, you can create type aliases and new class types, but not new fundamental types.

    As a compiler implementer sure, you could.

    Is it possible to define a customized qualifier

    Not as a user of the language.