Boost logo

Boost :

From: scleary_at_[hidden]
Date: 1999-11-19 09:00:30


Microsoft hates me. Fortunately, the feeling's mutual. :)

        -Steve

-- Begin empty_member.html ---

<html>
<head>
<title>
Empty Member Optimization
</title>
<body>

<h1>Empty Member Optimization</h1>

<p>
<h2>Header File</h2>

<p>
The empty_member class is in the header file <a
href="empty_member.h">empty_member.h</a>.

<p>
<h2>Introduction</h2>

<p>
When implementing some classes, the objects often need to keep a member
object of a template type which may or may not be an empty class. A good
example is the STL containers needing to keep copies of their allocator
objects, which are normally empty classes.

<p>
This will cause wasted space if the classes are empty because the ANSI
specification requires member subobjects of class type to have nonzero size
[ANSI 9.0.3]. However, the ANSI specification does not require empty base
class subobjects to have nonzero size [ANSI 9.0.3, Note 94].

<p>
The solution is to bundle the possibly empty class type with another member
of the same object whose size is known to be nonzero. This solution was
first invented by Nathan C. Myers (<a
href="http://www.cantrip.org/">www.cantrip.org</a>) and his paper on the
subject may be found at <a
href="http://www.cantrip.org/emptyopt.html">http://www.cantrip.org/emptyopt.
html</a>.

<p>
<h2>Classes</h2>

<p>
<h3>empty_member</h3>
<p>
<h4>Template Parameters</h4>
<p>
<b>Base</b>: The (possibly empty) base class that empty_member is to derive
from
<p>
<b>Member</b>: The (not empty) member class that empty_member is to hold;
<b>Member</b> may be a reference type
<p>
<h4>Typedefs</h4>
<p>
base_type: The template parameter <b>Base</b>
<p>
member_type: The template parameter <b>Member</b>
<p>
<h4>Public Data Members</h4>
<p>
m: An object of type <b>Member</b>
<p>
<h4>Constructors</h4>
<p>
empty_member(): Initializes the base class and member object with their
default constructors.

<p>
explicit empty_member(const Base & nb): Initializes the base class with
<i>nb</i> and the member object with its default constructor.

<p>
explicit empty_member(const Member & nm): [For non-reference types of
<b>Member</b> only] Initializes the base class with its default constructor
and the member object with <i>nm</i>.

<p>
explicit empty_member(Member & nm): [For reference types of <b>Member</b>
only] Initializes the base class with its default constructor and the member
reference with <i>nm</i>.

<p>
empty_member(const Base & nb, const Member & nm): [For non-reference types
of <b>Member</b> only] Initializes the base class with <i>nb</i> and the
member object with <i>nm</i>.

<p>
empty_member(const Base & nb, Member & nm): [For reference types of
<b>Member</b> only] Initializes the base class with <i>nb</i> and the member
object with <i>nm</i>.

<p>
Note: There is an ambiguity danger with the single-parameter constructors;
if <b>Base</b> could be the same class as <b>Member</b>, then always use the
constructor which takes both arguments.

<hr>
Note: There is no danger of ambiguous name lookup resulting if the Base
class has a member named "m". This is because the ANSI specification
requires that base class members "hide" their parent class members of the
same name [ANSI 10.2.2].

<hr>
A Note on Usage:<br>
empty_member should be used in a consistent fashion to increase readability.
The recommended usage is as follows (the names are taken from STL
concepts):<br>
Given a class Container, templated on a type T and a (possibly empty) type
Allocator, let us suppose that Container wishes to hold an object of type
Allocator named "alloc" and an object of type "T *" named "ptr". Then the
Container would declare an empty_member&lt;Allocator, T&gt; named some
unique name, say "_m1". Container should then declare, at appropriate
access levels, functions with the objects' originally intended names, as
follows: "T & ptr() { return _m1.m; }", "const T & ptr() const { return
_m1.m; }", "Allocator & alloc() { return _m1; }", "const Allocator & alloc()
const { return _m1; }", which allows access to the objects through their
proper names.
<p>
This technique is used because it is not unusual to have unrelated objects
grouped in empty_members.

</body>
</html>

-- End empty_member.html ---


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk