Overloading in C++, part I
Friday, February 23, 2007
The following are the things we need to know about overloading:
What can be overloaded?
[1] Only function declarations can be overloaded; object and type declarations cannot be overloaded.
[2] Function declarations that differ only in return type cannot be overloaded.
void f();
int f(); // violation of [2]
Static members and the implicit object
[3] Member function declarations with the same name and the same parameter types cannot be overloaded if any of them is a static member function declaration.
class X
{
public:
static void f();
void f() const; // violation of [3]
void f() const volatile; // violation of [3]
};
[3.1] If there is no static member function declaration among a set of member function declarations with the same name and the same parameter types, then these member function declarations can be overloaded if they differ in the type of their implicit object parameter.
class X
{
public:
void f();
void f() const; // valid
void f() const volatile; // valid
void g() const; // violation of [3.1]
void g() const; // violation of [3.1]
};
Equivalent Parameter Declaration
[4] Function declarations that have equivalent parameter declarations declare the same function and therefore cannot be overloaded.
[4.1] Parameter declarations that differ only in the use of equivalent typedef "types" are equivalent. A typedef is not a separate type, but only a synonym for another type.
typedef int Integer;
class X
{
public:
void f( int a );
void f( Integer b ); // violation of [4.1]
};
[4.2] Enumerations are distinct types and can be used to distinguish overloaded function declarations.
enum E { SOMETHING, NOTHING };
class X
{
public:
void f( int a );
void f( E b ); // valid
};
[4.3] Parameter declarations that differ only in a pointer * versus an array [] are equivalent. That is, the array declaration is adjusted to become a pointer declaration.
class X
{
public:
void f( char* a );
void f( char b[] ); // violation of [4.3]
void f( char c[20] ); // violation of [4.3]
};
[4.4] Parameter declarations that differ only in that one is a function type and the other is a pointer to the same function type are equivalent. That is, the function type is adjusted to become a pointer to function type.
class X
{
public:
void f( int() );
// redeclaration of f( int() )
void f( int (*)() );
// definition of f( int() )
void f( int() ) {}
// ill-formed: redefinition of f( int() )
void d( int (*)() ) {}
};
[4.5] Parameter declaration that differ only in the presence or absence of const and/or volatile are equivalent. That is, the const and volatile type-specifiers for each parameter type are ignored when determining which function is being declared, defined, or called.
class X
{
public:
void f( int a ) {}
// violation of [4.5]
void f( const int b ) {}
};
[4.5.1] Only the const and volatile type-specifiers at the outermost level of the parameter type specification are ignored in this fashion; const and volatile type-specifiers buried within a parameter type specification are significant and can be used to distinguish overloaded function declarations. In particular, for any type T, "pointer to T," "pointer to const T," and "pointer to volatile T" are considered distinct parameter types, as are "reference to T," "reference to const T," and "reference to volatile T."
class X
{
public:
void f( int a ) {}
void f( const int* b ) {} // valid
void f( float c ) {}
void f( const float& d ) {} // valid
};
[4.6] Two parameter declarations that differ only in their default arguments are equivalent hence cannot be overloaded.
class X
{
public:
void f( int a, int b = 100 ) {}
// violation of [4.6]
void f( int c, int d = 222 ) {}
};
C/C++ Glossary
Friday, November 24, 2006
Definitions take directly from MSDN Online Voices "Deep C++" column, C++ Templates: The Complete Guide, and the C++ Standard: ISO/IEC 14882:2003.
ANSI
The American National Standards Institute technical committee, once designated X3J11 but now known simply as J11, is responsible for creating and maintaining the U.S. C programming language standard. A similar committee, J16, is responsible for the collateral U.S. C++ standard. Both committees represent the U.S. on the corresponding ISO committees.
ANSI C
The C language specified in ANSI's 1989 C standard, and largely inherited from K&R C. Also known as C89 (from its year of adoption).
ANSI C standard
Formal document name: ANSI X3.159-1989. The U.S. C language standard published by ANSI in 1989, technically equivalent to—and supplanted by—the ISO C standard published a year later.
The ARM
Acronym for The Annotated C++ Reference Manual, written by Margaret Ellis and Bjarne Stroustrup, and first published in 1991. The ARM is to standard C++ as K&R is to standard C: The de facto standard of its day, and the foundation for the eventual ISO standard.
argument deduction
Instead of always requiring explicit <...> function template arguments, compilers may deduce those template arguments from a function call's context. The compiler attempts to match the call's argument type(s) against the parameter patterns in candidate function templates (and their possible explicit specializations). If a single unambiguously best match exists, the function is implicitly instantiated.
Argument deduction allows calls to template functions to look like calls to non-template functions. However, the deduction algorithm is not trivial, and the compiler cannot always deduce a best match. Conversely, the compiler may deduce a match that is not what you expect, since the template argument-to-parameter conversion rules differ from other C++ conversion rules.
automatic storage duration, automatic object
A C or C++ local-scope object explicitly declared auto or register, or not explicitly declared extern or static, has automatic storage duration. An object with such storage duration is an automatic object. Storage for these objects lasts until the block in which they are created exits. Automatic objects are what most programmers think of as "local variables" or "stack variables."
CV qualifiers
Standard-ese for the const and volatile type qualifiers.
D&E
Acronym for The Design and Evolution of C++, written by Bjarne Stroustrup, and first published in 1994. While the C and C9x standards have corresponding Rationale documents, the C++ standard does not. Instead the D&E serves as the de facto Rationale for standard C++.
declarator
Simplistic definition: The "right side" of a C or C++ declaration.
Robust definition: Within a C or C++ declaration, the name of a single object, function, or typedef, plus possible associated modifiers:
*s and &s
cv-qualifiers (const and volatile)
name specifiers (like std::)
function parameters within ()
array lengths within []
exception specification
initializer
Examples:
&a[N]
* const p = 0
X::f(int) throw()
Declarators are a Big Deal—the entire clause 8 of the C++ standard is devoted to them.
Along with declaration specifiers (the "left side" of declarations), declarators comprise the most convoluted and error-prone syntax in the C and C++ grammars. When confronted by obscure compile-time declarator errors, even seasoned programmers will randomly throw in extra syntax—typically *s and ()s—hoping that the errors will magically go away.
dynamic storage duration, dynamically created/destroyed object
A C++ object is dynamically created via a new expression, and dynamically destroyed via a delete expression. Such objects have dynamic storage duration; their storage lasts until freed by operator delete or operator delete[].
dynamic type
The type of the most derived object to which the lvalue denoted by an lvalue expression refers.
Example:
If a pointer p whose static type is “pointer to class B” is pointing to an object of class D, derive from B, the dynamic type of the expression *p is “D.” References are treated similarly.
class B { };
class D : public B { };
D d;
B* p = &d; // dynamic type of *p is D
The dynamic type of an rvalue expression is its static type.
EH
Shorthand notation for standard C++ exception handling.
fully constructed
A C++ object is fully constructed if, and only if, its constructor has completed and its destructor has not begun. Full construction is aborted if the constructor throws an exception.
Since contained subobjects construct before the containing constructor begins, the property of full construction is recursive: If one subobject deep in a class hierarchy throws an exception, all nested containing objects up the chain fail to construct unless/until the exception is handled. Caveat Constructor.
ISO
Also known as the International Organization for Standardization. Contrary to popular belief, the name ISO is not an acronym, but derives from the Greek "isos" meaning "equal." (The English prefix "iso-" also derives from this same word.)
ISO committees JTC1/SC22/WG14 and WG21 are responsible for creating international C and C++ language standards, respectively. The committees comprise representatives from national standards bodies; in the United States, those national bodies are ANSI committees J11 and J16.
ISO C standard, a.k.a C standard
Formal document name: ISO/IEC 9899:1990. There is also a corresponding Rationale.
This document is the international C language standard published by ISO in 1990. It is technically identical to the ANSI C standard; however, the two published standards use slightly different nomenclature and section numbering.
Since 1990, ISO has updated the C standard with three addenda:
ISO/IEC 9899 AM1. 1995 Amendment 1, adding international character support. Also known as Normative Addendum 1.
ISO/IEC 9899 TCOR1. 1995 Technical Corrigendum 1, correcting technical errors in the standard.
ISO/IEC 9899 TCOR2. 1996 Technical Corrigendum 2, correcting a smaller number of additional technical errors.
The C standard is not free, nor can you purchase it from ISO. You must instead purchase it from either your nation's ISO member body or a reseller.
ISO C9x standard (Final Committee Draft)
Formal document name: WG14/N843. There is also a corresponding Rationale.
ISO working group JTC1/SC22/WG14 is currently revising the entire C standard. The C language specified by this revised standard is colloquially called C9x. As the name suggests, C9x standardization is scheduled for completion in the 1990's.
ISO C++ standard, a.k.a. C++ standard
Formal document name: ISO/IEC 14882:1998. This document is the international C++ language standard published by ISO in 1998.
K&R
Shorthand for Brian Kernighan and Dennis Ritchie's book The C Programming Language, first edition. Published in 1978, K&R formed the basis of the ANSI C standard introduced a decade later.
K&R C
The C language specified in K&R. K&R C lacks several key features of standard C: function prototypes, const and volatile keywords, void type, enumeration types, and a well-defined library.
lvalue
Literally an "l value" or "left value." So-called because, in K&R C, an lvalue can appear on the left side of an assignment expression. In standard C or C++, an lvalue is more properly a "locator value" designating an object.
Lvalues are either modifiable or non-modifiable, a concept generally mapping to non-const and const objects, respectively. Their names aside, non-modifiable (const) lvalues cannot appear on the left in an assignment. (Because K&R C does not have the const keyword, all K&R lvalues are modifiable.)
name decoration or name mangling
Encoding of C++ names into C pseudo-names discernible by C linkers. In particular, such mangling allows C linkers to support class/namespace scope and function overloading, by turning what would be invalid redefinition of the same name into a new definition of a unique synthetic name.
The C++ standard does not regulate the algorithm mapping between original C++ names and linker-friendly mangled names. Instead, each translator vendor is free to create a unique naming scheme. This suggests that object files with different name-mangling schemes cannot be mixed.
(Note that other considerations—parameter-passing method, register allocation, stack alignment—also prevent inter-vendor object file mixing. Name mangling just takes a bad interoperability situation and makes it worse.)
overloaded declarations
When two or more different declarations are specified for a single name in the same scope, that name is said to be overloaded. Two declarations in the same scope that declare the same name but with different types.
overload resolution
When an overloaded function name is used in a call, which overloaded function declaration is being referenced is determined by comparing the types of the arguments at the point of use with the types of the parameters in the overloaded declarations that are visible at the point of use. This function selection process is called overload resolution.
partially constructed
A C++ object is partially constructed if, and only if, its constructor has not finished execution.
RTTI
Literally "Run-Time Type Identification," the standard C++ mechanism for class objects to identify their dynamic or run-time types. RTTI is supported by the keywords dynamic_cast and typeid and the standard library header
rvalue
Literally an "r value" or "right value." Rvalues always appear on the right side of an assignment statement. Unlike lvalues, which designate objects, rvalues designate values only—at least in C. C++ also has "class rvalues" designating unnamed temporary objects of a constructed (class) type; such objects are conceptually values of that type.
Standard C
The C language specified in the ISO C standard. Technically identical to ANSI C.
Standard C++
The C++ language specified in the ISO C++ standard.
Standard template library (STL)
A collection of C++ class and function templates, originally created by Stepanov, et al. at Hewlett-Packard, and later modified for and incorporated into the C++ standard library.
The STL is really two distinct things:
a set of rigorously-defined design patterns
representations of those patterns
The design patterns include:
object containers
algorithms manipulating container elements
iterators accessing container elements
allocators managing container memory
adapters giving containers and algorithms new interfaces
The C++ standard library implements many templates that hew to the STL design patterns. Indeed, many programmers are content to use just the library's implementation. However, if you follow the design-pattern rules, you can create containers, algorithms, and so on, that interact in complex yet predictable ways with the standard library.
static type
The type of an expression, which type results from analysis of the program without considering execution semantics. The static type of an expression depends only on the form of the program in which the expression appears, and does not change while the program is executing.
storage-class specifier
In standard C, any of the keywords
auto
extern
register
static
In standard C++, the above keywords plus
mutable
As the term suggests, a storage class specifier generally describes what kind of storage objects occupy, how long that storage exists, and how visible the storage is. The anomaly is mutable, which really doesn't describe storage at all. However, since mutable can appear in exactly the same grammatical contexts as the other "real" storage class specifiers, considering mutable a storage class specifier does simplify the C++ grammar.
subobject
A C++ object contained by other objects. A subobject is a named data member object, an unnamed base class object, or an array element.
Ant. complete object.
template
A construct that represents a family of classes or functions. It specifies a pattern from which actual classes or functions can be generated by substituting the template parameters by specific entities.
template argument
The "value" substituted for a template parameter. This "value" is usually a type, although certain constant values and templates can be valid template arguments too.
template argument deduction
The process that implicitly determines the template arguments from the context in which template arguments are used.
template-id
The combination of a template name followed by template arguments in angle brackets.
template parameter
A generic placeholder in a template. The most common kind of template parameter are type parameters, which represent types. Non-type parameters represent constant values of a certain type, and a template template parameters represent class templates.
type qualifiers
To the CV qualifiers const and volatile, C9X adds a third: restrict. Rather than call the resulting set of attributes "CRV qualifiers" or some such, the C9X standard committee elects to call them simply type qualifiers.