Dated Jun 4, 2022;
last modified on Sat, 04 Jun 2022
Definition from C++
Built-in types are ones that can be built from the fundamental types
(e.g. void, std::nullptr_t, int, bool, char, float, double), the const modifier, and
the declarator operators (e.g. int[3], int*, int&).
While the built-in types directly and efficiently represent the
capabilities of conventional computer hardware, they’re too low-level to
conveniently write advanced applications in. C++ abstraction mechanisms
let programmers design and implement user-defined types using both
built-in types and other user-defined types.
Before this, I thought user-defined types did not include STL types. I
thought that user meant me, or library authors, but not the C++ language
devs, who, I assume, write the STL.
User-defined types are often easier to use, less error prone, and
typically as efficient as direct use of built-in types.
says that sometimes user-defined types
can be more efficient than built-in types. When would this be? Maybe
something like
std::vector<bool>,
which is possibly (implementation-defined) more space-efficient than a
std::vector (and thus an array) of bool?
A Tour of C++ (Second Edition).
Chapter 2. User-Defined Types.
Bjarne Stroustrup.
2018.
ISBN: 978-0-13-499783-4 .
Fundamental types - cppreference.com.en.cppreference.com.
Accessed Jun 4, 2022.
quotes Doug McIlroy:
Those types are not “abstract”; they are as real as int and float.
What is the context of this quote?
A class is a user-defined type provided to represent a concept in the code of a program. Essentially, all language facilities beyond the fundamental types, operators, and statements exist to help define better class or to use them more conveniently....
A struct helps us organize the elements that a type needs into a data structure, e.g.
struct Vector { int sz; // number of elements double* elem; // pointer to elements on the free store }; void vector_init(Vector& v, int s) { v.elem = new double[s]; v.sz = s; }
However, notice that a user of Vector has to know every detail of a Vector’s representation. We can improve on this....
Unscoped (or Plain or C-Style) Enumerations Plain (or C-style) enums are entered in the same scope as the name of their enum, and implicitly convert to their integer value, e.g.
enum Color { red, green, blue }; int col = green; Color c2 = 1; // error: invalid conversion from 'int' to 'Color' [-fpermissive] enum CardColor { red, black }; // Error: "red" conflicts with a previous declaration...
A union is a struct in which all members are allocated at the same address so that the union occupies as much space as its largest member, e.g.
// This is a naked union because it doesn't have an associated indicator // for which member it holds. union Value { Node* p; int i; } // The language doesn't keep track of which kind of value is held by a // union, so the programmer must do that themselves, e....
Mixins Introduced to mixins by . Didn’t know that there are passionate advocates for this, e.g., ’s “You can even look at normal subclass inheritance as a degenerate form of mixin inheritance where the superclass is known at class definition time, and there’s only one application of it.”
A mix-in is an abstract subclass. This technique is especially useful in languages where a class can only have a single superclass....
Before this, I thought user-defined types did not include STL types. I thought that user meant me, or library authors, but not the C++ language devs, who, I assume, write the STL.