C++ inherits from C the sizeof operator; it yields the number of bytes in the object representation of its operand. It comes in two flavours, `sizeof( type-name )` and `sizeof expression`. It's important to note that expressions in the form `sizeof( ptr )->member` refer to the second sizeof form, and are not a syntactic error.
While sizeof is just about getting the size of its operand, its power extends beyond that.
Friday, December 25, 2009
Sunday, November 29, 2009
Episode Twelve: When the Size *does* Matter
In C++, zero-sized addressable objects are forbidden. Objects must have size, even if they are empty. This is required in order to have different addresses for different objects. From the standard, 1.8.5:
A most derived object shall have a non-zero size and shall occupy one or more bytes of storage. Base class subobjects may have zero size.
Saturday, October 24, 2009
Episode Eleven: Exceptions for the Exceptional
An exception breaks the normal flow of code under given conditions, and forces the caller to deal with them, as opposed to return codes. In C++, exceptions should be used for the exceptional situations only. An exceptional situation is something that should never happen (yet it might). This is not the situation with other languages; notably Python throws an exception to signal the end of an iteration.
Saturday, October 17, 2009
Episode Ten: The Natural Order of Things
While each statement is evaluated after the previous one, the evaluation of individual sub-expressions is left unspecified in C++. This is supposed to allow producing optimal code for the target platform, as opposed to intuitive left-to-right evaluation everywhere. Code relying on the implementation defined evaluation order is thus non-portable. This means that `v[i] = i++`, `v[i] + i++` and even `f(v[i], i++)` are all examples of non-portable code. Generally speaking, when a variable is read twice and also written in the same expression the result is undefined.
The standard defines a few exceptions where evaluation order is specified:
The standard defines a few exceptions where evaluation order is specified:
Wednesday, September 23, 2009
Episode Nine: Erasing the Concrete
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.
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.
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.
(...)
Saturday, July 18, 2009
Episode Three: Friends with Benefits
Friendship is a special relation among classes in C++. A friend of a class is a function or class that is given access to its private (and protected) parts.
Friday, July 17, 2009
Episode Two: Those Whose Names must not be Spoken
The C++ standard reserves a set of names for use by C++ implementation and standard libraries [C++ standard 17.6.3.3 - Reserved names]. Those include but are not limited to:
- Names containing a double underscore.
- Names that begin with an underscore followed by an uppercase letter.
- Names that begin with an underscore at the global namespace.
- Names containing a double underscore.
- Names that begin with an underscore followed by an uppercase letter.
- Names that begin with an underscore at the global namespace.
Wednesday, July 15, 2009
Episode One: To be or not to be const
Const-correctness is the form of program correctness that deals with the proper declaration of objects as mutable or inmutable. It dictates that all objects should be declared as const unless they need to be modified. It results in clearer, easier to follow code.
Subscribe to:
Posts (Atom)