Type erasure is a technique that allows to store and operate objects of different types that fulfill a given concept. It can be seen as non-intrusively introducing an abstract base class to every type that provides a common interface. It turns a wide variety of types into one type.
Two big exponents of type erasure are Boost.Any [1] and Boost.Function [2]. Boost.Any is like C# and Java's Object base class, except it has value semantics. Boost.Function implements a generalized callback type that can be used to store arbitrary function pointers and function objects.
Wednesday, September 23, 2009
Friday, September 11, 2009
Episode Eight: The Curious Case of Recurring Template Pattern
The curiously recurring template pattern (CRTP) is an idiom in which a class X derives from a class template instantiation using X itself as template argument. It splits generic and concrete functionality by delegating to its derived class. This works because X is an incomplete type at the point of template instantiation, and it only works when the declaration of the base template does not use X as a complete type. Member function definitions, on the other side, are instantiated long after their declarations, allowing use of members of the derived class via the use of a static_cast.
Friday, August 28, 2009
Episode Seven: One char to Rule Them All
There are three char types in C++: one is signed, one is unsigned, and the other one is neither of those. From the standard, 3.9.1.1:
Characters can be explicitly declared unsigned or signed. Plain char, signed char, and unsigned char are three distinct types. A char, a signed char, and an unsigned char occupy the same amount of storage and have the same alignment requirements (3.11); that is, they have the same object representation. For character types, all bits of the object representation participate in the value representation.
Wednesday, August 19, 2009
Episode Six: Boolsh*t
The built-in type bool is an integral type that can have one of the two values `true` or `false`. Despite being an integral type there are no signed, unsigned, short, or long bool types or values. There are some special situations where extra care is needed when working with bools:
Saturday, August 8, 2009
Episode Five: Explicit is Better than Implicit
Type conversion allows one type to convert to another. In C++, they came in two flavours:
- Conversion by construction: Constructors that can be called with a single argument are called converting constructors. This includes constructors with a single formal argument, as well as constructors with more than one argument that specifies default values. Implements conversion-from-type, intrusive on the destination type.
- Conversion functions: Also called conversion operators, are member functions defined as `operator Type() [const]` (Note no result type defined). Implements conversion-to-type, intrusive on the source type.
Wednesday, July 22, 2009
Episode Four: Not Enough Keys in the Keyboard
The basic character set of the C programming language is a subset of the ASCII character set that includes nine characters which lie outside the ISO 646 invariant character set. Trigraphs were invented as a way of entering source code using keyboards that support any version of the ISO 646 character set.
Monday, July 20, 2009
Breaking News: Concepts are out
Beman Dawes posted earlier today at the Boost Developers Mailing List:
The C++ standards committee met in Frankfurt, Germany, last week. Key actions:We'll have to keep waiting...
* Concepts have been removed from C++0x.
The committee is very much in favor of Concepts. But Concepts are seen as so important that they have to be right. And the strong consensus was that getting Concepts right, for any reasonable definition of "right", would add several years to the schedule. There was also a consensus that an implementation of a compiler that could compile the entire standard library is part of the definition of "right". While ConceptGCC has been a great help, it isn't there as far as validating "right". Personally, I think Clang may end up being the compiler that eventually validates Concepts.
(...)
Subscribe to:
Posts (Atom)
