Appendix Two

Terminology

The terminology used by this book is as defined by the "Draft Standard for The Programming Language C++" with some additions that are presented below.

abstract base class

An abstract base class is a class with at least one pure virtual member function.

access function
accessor

An access function (accessor) is a member function that returns a value and that does not modify the object's state.

built-in type

A built-in type is one of the types defined by the language, such as int, short, char and bool.

CLASS invariant

A class invariant is a condition that defines all valid states for an object. An class invariant is both a pre- and postcondition to a member function of the class.

const correct

A program is const correct if it has correctly declared functions, parameters, return values, variables and member functions as const.

copy assignment
operator

The copy assignment operator of a class is the assignment operator taking a reference to an object of the same class as parameter.

copy constructor

The copy constructor of a class is the constructor taking a reference to an object of the same class as parameter.

dangling pointer

A dangling pointer is pointing at an object that been deleted.

declarative region

A declarative region is the largest part of a program where a name declared in that region can be used with its unqualified name.

direct base class

The direct base class of a class is the classes explicitly mentioned as base classes in its definition. All other base classes are indirect base classes .

dynamic binding

A member function call is dynamically bound if different functions will be called depending on the type of the object operated upon.

encapsulation

Encapsulation allows a user to only depend on the class interface, and not upon its implementation.

exception safe

A class is exception safe if its objects do not loose any resources, do not invalidate their class invariant or terminate the application when they end their life-time because of an exception.

explicit type
conversion

An explicit type conversion is when an object is converted from one type to another, and where you have to explicitly write the resulting type.

file scope

An object with file scope is only accessible to functions within the same translation unit.

flow control  primitive

The flow control primitives are:
if-else , switch , do­-while , while and for .

forwarding function

A forwarding function is a function which does nothing more than call another function.

free store

An object on the free store is an object allocated with new .

global object

A global object is an object in global scope.

global scope

An object or type is in global scope if it can be accessed from within any function of a program.

implementation-defined behavior

Code with Implementation-defined behavior is completely legal C++, but compilers may differ. The compiler vendor is required to describe what their particular compiler does with such code.

implicit type
conversion

An implicit type conversion is when an object is converted from one type to another, and where you don't have to explicitly write the resulting type.

inheritance

A derived class inherits state and behavior from a base class.

inline definition file

An inline definition file is a file that only contains definitions of inline functions.

iterator

An iterator is an object used to traverse through collections of objects.

literal

A literal is a sequence of digits or characters that represent a constant value.

member object

The member objects of a class is its base classes and the data members.

modifying function
modifier

A modifying function (modifier) is a member function that changes the value of at least one data member.

non-copyable class

A class is non-copyable if its objects cannot be copied.

object-oriented
programming

A language supports object-oriented programming if it provides encapsulation, inheritance and polymorphism.

polymorphism

Polymorphism means that an expression can have many different interpretations depending on the context. This means that the same piece of code can be used to operate upon many types of objects as provided by e.g. dynamic binding and parameterization.

postcondition

A postcondition is a condition that must be true on exit from a member function, if the precondition was valid on entry to that function. A class is implemented correctly if postconditions are never false.

precondition

A precondition is a condition that must be true on entry to a member function. A class is used correctly if preconditions are never false.

resource

A resource is something that more than one program needs, but for which there is a limit for how much that is available. Resources can be acquired and released.

self-contained

A header file is self contained if nothing more than its inclusion is needed to use the full interface of a class.

signature

The signature of a function is defined by its return type, its parameter types and their order, and the access given the object operated upon (const or volatile).

slicing

Slicing means that the data added by a subclass is discarded when an object of a subclass is passed or returned by value to or from a function expecting a base class object.

stack unwinding

Stack unwinding is the process during exception handling when the destructor is called for all local objects between the place where the exception was thrown and where it is caught.

state

The state of an object is the data members of the object, and possibly also other data which the object has access to, which affects the observable behavior of the object.

substitutability

Substitutability means that a derived class object can be used in a context expecting an object of any class derived from one its base class.

template 
definition file

An template definition file is a file that only contains definitions of non-inline template functions.

translation unit

A translation unit is the result of merging a implementation file with all its headers and header files.

undefined behavior

Code with undefined behavior is not correct C++. The standard does not specify what a compiler shall do with such code. It may ignore the problem completely, issue an error or something else.

unspecified behavior

Code with unspecified behavior is completely legal C++, but compilers may differ. The compiler vendor is not required to describe what their particular compiler does with such code.

user-defined
conversion

A user-defined conversion is a conversion from one type to another introduced by a programmer, i.e. not one of the conversions defined by the language. Such user-defined conversions are either a non- explicit constructor taking only one parameter, or a conversion operator.

virtual table

A virtual table is an array of pointers to all virtual member functions of a class. Many compilers generate such tables to implement dynamic binding of virtual functions.

Rules and recommendations     

Naming
Meaningful names

Rec 1.1 Use meaningful names.

Rec 1.2 Use English names for identifiers.

Rec 1.3 Be consistent when naming functions, types, variables and constants.

Names that collide

Rec 1.4 Only namespace names should be global.

Rec 1.5 Do not use global using declarations and using directives inside header files.

Rec 1.6 Prefixes should be used to group macros.

Rec 1.7 Group related files by using a common prefix in the file name.

Illegal naming

Rule 1.8 Do not use identifiers that contain two or more underscores in a row.

Rule 1.9 Do not use identifiers that begin with an underscore.

Organizing the code

Rule 2.1 Each header file should be self-contained.

Rule 2.2 Avoid unnecessary inclusion.

Rule 2.3 Enclose all code in header files within include guards.

Rec 2.4 Definitions for inline member functions should be placed in a separate file.

Rec 2.5 Definitions for all template functions of a class should be placed in a separate file.

Comments

Rec 3.1 Each file should contain a copyright comment.

Rec 3.2 Each file should contain a comment with a short description of the file content.

Rec 3.3 Every file should declare a local constant string that identifies the file.

Rec 3.4 Use // for comments.

Rec 3.5 All comments should be written in English.

Control flow

Rule 4.1 Do not change a loop variable inside a for -loop block.

Rec 4.2 Update loop variables close to where the loop-condition is specified.

Rec 4.3 All flow control primitives ( if, else, while, for, do, switch and case ) should be followed by a block, even if it is empty.

Rec 4.4 Statements following a case label should be terminated by a statement that exits the switch statement.

Rec 4.5 All switch statements should have a default clause.

Rule 4.6 Use break and continue instead of goto .

Rec 4.7 Do not have too complex functions.

Object Life Cycle
Initialization of variables and constants

Rec 5.1 Declare and initialize variables close to where they are used.

Rec 5.2 If possible, initialize variables at the point of declaration.

Rec 5.3 Declare each variable in a separate declaration statement.

Rec 5.4 Literals should only be used in the definition of constants and enumerations.

Constructor initializer lists

Rec 5.5 Initialize all data members.

Rule 5.6 Let the order in the initializer list be the same as the order of declaration in the header file. First base classes, then data members.

Rec 5.7 Do not use or pass this in constructor initializer lists.

Copying of objects

Rec 5.8 Avoid unnecessary copying of objects that are costly to copy.

Rule 5.9 A function must never return, or in any other way give access to, references or pointers to local variables outside the scope in which they are declared.

Rec 5.10 If objects of a class should never be copied, then the copy constructor and the copy assignment operator should be declared private and not implemented.

Rec 5.11 A class that manages resources should declare a copy constructor, a copy assignment operator, and a destructor.

Rule 5.12 Copy assignment operators should be protected from doing destructive actions if an object is assigned to itself.

Conversions

Rec 6.1 Prefer explicit to implicit type conversions.

Rec 6.2 Use the new cast operators ( dynamic_cast, const_cast, reinterpret_cast and static_cast ) instead of the old-style casts, unless portability is an issue.

Rec 6.3 Do not cast away const.

Rule 6.4 Declare a data member as mutable if it must be modified by a const member function.

The class interface
Inline functions

Rec 7.1 Make simple functions inline.

Rule 7.2 Do not declare virtual member functions as inline .

Argument passing and return values

Rec 7.3 Pass arguments of built-in types by value, unless the function should modify them.

Rec 7.4 Only use a parameter of pointer type if the function stores the address, or passes it to a function that does.

Rec 7.5 Pass arguments of class types by reference or pointer.

Rule 7.6 Pass arguments of class types by reference or pointer, if the class is meant as a public base class.

Rule 7.7 The copy assignment operator should return a non-const reference to the object assigned to.

Const Correctness

Rule 7.8 A pointer or reference parameter should be declared const if the function does not change the object bound to it.

Rule 7.9 The copy constructor and copy assignment operator should always have a const reference as parameter.

Rule 7.10 Only use const char -pointers to access string literals.

Rule 7.11 A member function that does not change the state of the program should be declared const .

Rule 7.12 A member function that gives non-const access to the representation of an object must not be declared const .

Rec 7.13 Do not let const member functions change the state of the program.

Overloading and default arguments

Rule 7.14 All variants of an overloaded member function should be used for the same purpose and have similar behavior.

Rec 7.15 If you overload one out of a closely-related set of operators, then you should overload the whole set and preserve the same invariants that exist for built-in types.

Rule 7.16 If, in a derived class, you need to override one out of a set of the base class' overloaded virtual member functions, then you should override the whole set, or use using-declarations to bring all of the functions in the base class into the scope of the derived class.

Rule 7.17 Supply default arguments with the function's declaration in the header file, not with the function's definition in the implementation file.

Conversion functions

Rec 7.18 One-argument constructors should be declared explicit .

Rec 7.19 Do not use conversion functions.

new and delete

Rule 8.1 delete should only be used with new .

Rule 8.2 delete [] should only be used with new [] .

Rule 8.3 Do not access a pointer or reference to a deleted object.

Rec 8.4 Do not delete this .

Rec 8.5 If you overload operator new for a class, you should have a corresponding overloaded operator delete .

Rec 8.6 Customize the memory management for a class if memory management is an unacceptably-large part of the allocation and deallocation of free store objects of that class.

Static Objects

Rec 9.1 Objects with static storage duration should only be declared within the scope of a class, function or anonymous namespace.

Rec 9.2 Document how static objects are initialized.

Object-oriented programming
Encapsulation

Rule 10.1 Only declare data members private.

Rec 10.2 If a member function returns a pointer or reference, then you should document how it should be used and for how long it is valid.

Dynamic binding

Rec 10.3 Selection statements ( if and switch ) should be used when the flow of control depends on an object's value, while dynamic binding should be used when the flow of control depends on the object's type.

Inheritance

Rule 10.4 A public base class must either have a public virtual destructor or a protected destructor.

Rule 10.5 If you derive from more than one base classes with the same parent, then that parent should be a virtual base class.

The Class Interface

Rec 10.6 Specify classes using preconditions, postconditions, exceptions and class invariants.

Rec 10.7 Use C++ to describe preconditions, postconditions and class invariants.

Rule 10.8 A pointer or reference to an object of a derived class should be possible to use wherever a pointer or reference to a public base class object is used.

Rec 10.9 Document the interface of template arguments.

Assertions

Rule 11.1 Do not let assertions change the state of the program.

Rec 11.2 Remove all assertions from production code.

Error handling
Different ways to report errors

Rec 12.1 Check for all errors reported from functions.

Rec 12.2 Use exception handling instead of status values and error codes.

When to throw exceptions

Rec 12.3 Only throw exceptions when a function fails to perform what it is expected to do.

Rec 12.4 Do not throw exceptions as a way of reporting uncommon values from a function.

Rule 12.5 Do not let destructors called during stack unwinding throw exceptions.

Rec 12.6 Constructors of types thrown as exceptions should not themselves throw exceptions.

Exception-safe code

Rec 12.7 Use objects to manage resources.

Rule 12.8 A resource managed by an object must be released by the object's destructor.

Rec 12.9 Use stack objects instead of free store objects.

Rec 12.10 Before letting any exceptions propagate out of a member function, make certain that the class invariant holds, and if possible leave the state of the object unchanged.

Exception types

Rec 12.11 Only throw objects of class type.

Rec 12.12 Group related exception types by using inheritance.

Rec 12.13 Only catch objects by reference.

Error recovery

Rule 12.14 Always catch exceptions the user is not supposed to know about.

Rec 12.15 Do not catch exceptions you are not supposed to know about.

Exception specifications

Rec 12.16 Use exception specifications to declare which exceptions that might be thrown from a function.

Parts of C++ to avoid
Library functions to avoid

Rec 13.1 Use new and delete instead of malloc , calloc , realloc and free .

Rule 13.2 Use the iostream library instead of C-style I/O.

Rule 13.3 Do not use setjmp( ) and longjmp() .

Rec 13.4 Use overloaded functions and chained function calls instead of functions with an unspecified number of arguments.

Language constructs to avoid

Rule 13.5 Do not use macros instead of constants, enums, functions or type definitions.

Rec 13.6 Use an array class instead of built-in arrays.

Rec 13.7 Do not use unions.

Size of executables

Rec 14.1 Avoid duplicated code and data.

Rule 14.2 When a public base class has a virtual destructor, each derived class should declare and implement a destructor.

Portability
General aspects of portability

Rule 15.1 Do not depend on undefined, unspecified or implementation-defined parts of the language.

Rule 15.2 Do not depend on extensions to the language or to the standard library.

Rec 15.3 Make non-portable code easy to find and replace.

Including files

Rule 15.4 Headers supplied by the implementation should go in <> brackets; all other headers should go in "" quotes.

Rec 15.5 Do not specify absolute directory names in include directives.

Rec 15.6 Include file names should always be treated as case sensitive.

The size and layout of objects

Rule 15.7 Do not make assumptions about the size of or layout in memory of an object.

Rule 15.8 Do not cast a pointer to a shorter quantity to a pointer to a longer quantity.

Rec 15.9 If possible, use plain int to store, pass or return integer values.

Rec 15.10 Do not explicitly declare integral types as signed or unsigned .

Rule 15.11 Make sure all conversions of a value of one type to another of a narrower type do not slice off significant data.

Rec 15.12 Use typedefs or classes to hide the representation of application-specific data types.

Unsupported language features

Rec 15.13 Always prefix global names (such as externally visible classes, functions, variables, constants, typedefs and enums) if namespace is not supported by the compiler.

Rec 15.14 Use macros to prevent usage of unsupported keywords.

Rec 15.15 Do not reuse variables declared inside a for -loop.

Other compiler differences

Rec 15.16 Only inclusion of the header file should be needed when using a template.

Rec 15.17 Do not rely on partial instantiation of templates.

Rec 15.18 Do not rely on the lifetime of temporaries.

Rec 15.19 Do not use pragma s.

Rule 15.20 Always return a value from main() .

Rec 15.21 Do not depend on the order of evaluation of arguments to a function.

Style
General Aspects of Style

Style 1.1 Do not mix coding styles within a group of closely related classes.

Naming conventions

Style 1.2 In names that consist of more than one word, the words are written together and each word that follows the first begins with an uppercase letter.

Style 1.3 The names of classes, typedefs, and enumerated types should begin with an uppercase letter.

Style 1.4 The names of variables and functions should begin with a lower-case letter.

Style 1.5 Let data members have a "M" as suffix.

Style 1.6 The names of macros should be in uppercase.

Style 1.7 The name of an include guard should be the name of the header file with all illegal characters replaced by underscores and all letters converted to uppercase.

Style 1.8 Do not use characters that can be mistaken for digits, and vice versa.

File-name extensions

Style 1.9 Header files should have the extension " .hh ".

Style 1.10 Inline definition files should have the extension " .icc ".

Lexical style

Style 1.11 The names of parameters to functions should be specified in the function declaration if the type name is insufficient to describe the parameter.

Style 1.12 Always provide an access specifier for base classes and data members.

Style 1.13 The public, protected, and private sections of a class should be declared in that order.

Style 1.14 The keyword struct should only be used for a C-style struct.

Style 1.15 Define inline member functions outside the class definition.

Style 1.16 Write unary operators together with their operand.

Style 1.17 Write access operators together with their operands.

Style 1.18 Do not access static members with ' . ' or ' -> '.

Symbols

"" 176

* 200

++ 200

. 200

<> 175

-> 200

_ 193, 195

__ 8

A

abort 138

abstract base class 108, 203

access function 203

access operator 200

access specifier

for base class 198-199

for data member 198-199

implicit 198

order of declaration 199

see also encapsulation

accessor 203

ambiguous name, multiple inheritance 113

argument passing 58-65, 66-67

passing array 164

array

see array class

see built-in array

array class, instead of built-in array 164

array, see built-in array

assertion

assert macro 130

conditions to check 120-122, 130-131

auto_ptr 147, 149

B

bad_exception 156

base class

abstract 108, 203

access specifier 198-199

destructor 110

duplicated 113

interface 107-108, 115

mix-in 111, 113

pointer or reference, using 110, 122

pointer, using 164

virtual 37

bitwise copying 39

block, after flow control primitive 27-28

break , see switch

built-in array

allocated with new 88-89

using 164

built-in type 203

alignment in struct 178

range of 179

size 178

C

C standard, see standard C

call chain 136

calloc 158

case 28

cast

different types 49

new-style 49

see also conversion

catch

as macro 183

base class reference 135, 151-153

integer 151

with ... 135, 146, 154

char

alignment of pointer 178

signed or unsigned 180

signed or unsigned 172

class

access specifiers 199

compared to struct 199

data member access 104-105, 198-199

factory class 108-110

forward-declaring 13-14

header file 15

name 192, 194

non-copyable 40, 205

resource management 41-42

singleton 97-98

specification 117, 120-122, 137

used for portability 180

see also base class

see also class interface

see also member function

class interface 116-127

avoiding exception 138

const correctness 72-74

exception specification 155

supplier, obligations 117

user, obligations 105, 117

class invariant 119-120, 203

as C++ expression 120

preserving 147

preserving in derived class 123

class template

documenting types 127

exception specification 156

instantiating 17, 186

see also template

code

duplicated 168-169

non-portable 174-175

organization 11-18

style 192

comment 19-23

C++ style and C style 22

copyright 20

file description 21

language 22

template parameter behavior 125

common identifier 6

compiler differences 184-190

const

cast away 52-??

correctness 66-74, 203

data member 162

instead of macro 194

member function 53, 70-74

parameter 66-67

see also mutable

const_cast , see new-style cast

constructor

assignment in body 35

declared explicit 183

expected behavior 147

implicit conversion 183

initializer list 35-38

inline 169

throwing exception 142, 147

see also new

control flow 25-30

complexity 30

primitive 27, 204

selection statements 107

with exception handling 137, 139

conversion 47-54

arithmetic 179

between pointer types 178

cast-expressions 49

explicit 204

explicit vs. implicit 48

from longer to narrower type 180

implicit 205

implicit with constructor 183

implicit with operator 160

using member function 48-49

conversion operator

for string class 160, 188

overloading 49, 82-83

returning pointer to data member 188

copy assignment operator 204

exception safe 148

return value 65-??

self-assignment 43, 45

type of parameter 67

when to implement 40

see also member function

copy constructor 204

throwing exception 142

type of parameter 67

when to implement 40

see also member function

copyable class 42

copyright comment 20

D

dangling pointer 204

how to avoid 43, 89

data member

access specifier 104-105, 198-199

constant 162

initialization 35

mutable 53

naming convention 194

offset 178

static 97

declaration

class 199

return value 189

variable 34

declarative region 204

default arguments 81

default int 189

default , see switch

delete

accessing deleted object 89

and new 88

derived class object 112

instead of free 158

stack object 90

when to call 106, 158

delete 88-91

derived class

compiler-generated destructor 170

delete through base pointer 112

implement base class interface 107-108

object, copying 151

overriding member functions 108

destructor

called after exception 134, 147

catching exception 140

compiler-generated 170

exception safe 147

explicit call 92

inline 169

mix-in class 111

releasing resource 145

virtual 111, 170

virtual or not 111

see also delete

see also member function

direct base class 204

do-while , when to use 27

dynamic binding 106-110, 204

instead of union 165

dynamic storage duration, see new

dynamic_cast , see new-style cast

E

encapsulation 104-106, 204

enum

anonymous 162

casting an integer to 173

instead of macro 194

instead of static const int 162

name 194

errno 135, 153

error handling 133-156

assertions and exceptions 130

code, location 145

constructor 136

error description 150-152

errors that cannot be prevented 137-140

inside destructor 142

overloaded operator 136

status values and error codes 134

unexpected value in switch 29

error recovery 154-155, 159

evaluation order

argument 189

subexpressions 189-190

exception

compared to status value 134-136

representing the type of error 150

specification 117

throwing class object 150

to terminate program 138

translation of status value 137

type 149-153, 155

unexpected 156

unhandled 134, 140

unknown type 154

when to throw 137-143

see also throw

exception class

constructor 142

copy assignment operator 148

copy constructor 142

destructor 140

error description 150-152

inheritance 135, 151

nested 151

exception handling 133-156

bad_exception 156

one catch for many exceptions 153

performance 148

preventing memory leaks 92

proper use 137-140

recover from exception 147, 154-155

resource management 145

rethrow exception 140

simulating 159

stack unwinding 140

terminate 140

uncaught_exception 140-141

unexpected exception 156

when to throw exception 137-143

exception safe 143-149, 204

exception specification 123, 155-156

to document class interface 155

explicit 183

extension

C++ 174

standard library 174

EXTERNAL_TEMPLATE_DEFINITION 187

F

factory class 108-110

false 183

file

description 21

identification 21

inclusion 175-177

name 8, 195-196

name, case sensitive 196

name, template implementation 186

scope 204

see also header file

see also inline definition file

see also template header file

finalization function 99

flow control primitive 27, 204

for

loop index 26

scope of loop variable 184

when to use 27

forwarding function 204

free 158

free store 204

see new and delete

function

calls, chained 159-160

complexity 30

declaration 189

evaluation of argument 189

lifetime of return value 187

local linkage 169

name 194

ownership of return value 106

returning reference or pointer 105

returning status value 134-135

unspecified number of arguments 159-160

wrapper 136

see also inline function

see also linker

see also template function

function parameter 59-65

const 66-67

name 194, 197-198

pointer to array 164-165

reference to temporary 188

reference vs. pointer 60-62

specifying constness 52

to document function 198

function template

exception specification 156

instantiating 17, 186

see also template

G

global

name 181

object 204

scope 204

goto 29-30

H

header file

extension 196

how to avoid multiple includes 14

inclusion 14, 175

name 195

name, case 176-177

path name separator 176

purpose 12

self-contained 12

when to include 13

see also template header file

header, inclusion of 175

I

identifier

language 2

separating words in 193

if-else

see control flow

if-else 27, 107

implementation file

extension 196

name extension 196

implementation-defined behavior 172, 205

char signed or unsigned 179

data member offset 178

include 176

include-directives 175

layout of object 177

lifetime of temporary object 187

pragma 188

size of object 177

subtracting with unsigned types 179

include

header 175

header file 175

path names 176

using "" 175

using <> 175

see also header file

see also template header file

include guard 14, 195

name 195

inheritance 110-115

instead of union 165

multiple 113-115

private, protected or public 122

purpose 122

replace if-else and switch 107

shared base class member 113

substitutability 122-124

initialization

data member 35

instead of assignment 33

object 100-102

order, static objects 98-102

order, within translation unit 99

using function 99

variable 33

initializer list 35-38

calling member function 38

order of initializers 37

passing this 37

inline definition file 15, 205

extension 196

inline function 56-58

effect on compile time 13, 57

effect on performance and size 56, 169

failure to inline 169

inline member function

in separate file 15, 200

virtual 57

inlining

compiler-differences 169

correct use 56-57

when to avoid 169

int

conversion from char 178

out of range 179

performance 179

size 178-179

INT_MAX 179

INT_MIN 179

integral suffix 195

integral types

choosing 179

signed and unsigned 179

invariant

see class invariant

iostream

compared to stdio 159

performance 159

ISO 9126 171

iterator 139-140, 205

K

keyword

as macro 182

list 182

unsupported 183

L

letters, case 193-196

limits.h