In
computer programming
Computer programming is the process of performing a particular computation (or more generally, accomplishing a specific computing result), usually by designing and building an executable computer program. Programming involves tasks such as anal ...
, a forward declaration is a
declaration of an
identifier
An identifier is a name that identifies (that is, labels the identity of) either a unique object or a unique ''class'' of objects, where the "object" or class may be an idea, physical countable object (or class thereof), or physical noncountable ...
(denoting an entity such as a type, a variable, a constant, or a function) for which the programmer has not yet given a complete
definition
A definition is a statement of the meaning of a term (a word, phrase, or other set of symbols). Definitions can be classified into two large categories: intensional definitions (which try to give the sense of a term), and extensional definitio ...
.
It is required for a
compiler
In computing, a compiler is a computer program that translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primarily used for programs that ...
to know certain properties of an identifier (size for
memory allocation
Memory management is a form of resource management applied to computer memory. The essential requirement of memory management is to provide ways to dynamically allocate portions of memory to programs at their request, and free it for reuse when ...
,
data type
In computer science and computer programming, a data type (or simply type) is a set of possible values and a set of allowed operations on it. A data type tells the compiler or interpreter how the programmer intends to use the data. Most progra ...
for type checking, such as
type signature
In computer science, a type signature or type annotation defines the inputs and outputs for a function, subroutine or method. A type signature includes the number, types, and order of the arguments contained by a function. A type signature is ty ...
of functions), but not other details, like the particular value it holds (in case of variables or constants) or definition (in the case of functions). This is particularly useful for
one-pass compiler
In computer programming, a one-pass compiler is a compiler that passes through the parts of each compilation unit only once, immediately translating each part into its final machine code. This is in contrast to a multi-pass compiler which convert ...
s and
separate compilation
Separate or separates may refer to:
*Soil separates, three kinds of soil mineral particles: sand, silt, and clay
*Separate (song), 2016 song by South African songstress Amanda Black
*Separates (clothing), Mix-and-match separates, clothing
* ''Sep ...
.
Forward declaration is used in languages that require declaration before use; it is necessary for
mutual recursion
In mathematics and computer science, mutual recursion is a form of recursion where two mathematical or computational objects, such as functions or datatypes, are defined in terms of each other. Mutual recursion is very common in functional progr ...
in such languages, as it is impossible to define such functions (or data structures) without a forward reference in one definition: one of the functions (respectively, data structures) must be defined first. It is also useful to allow flexible code organization, for example if one wishes to place the main body at the top, and called functions below it.
In other languages forward declarations are not necessary, which generally requires instead a
multi-pass compiler and for some compilation to be deferred to
link time
In computer science, link time refers to the period of time, during the creation of a computer program, in which a linker is being applied to that program. Link time occurs after compile time and before runtime (when a program is executed).
It ...
. In these cases identifiers must be defined (variables initialized, functions defined) before they are used in execution, but do not need to be defined before they are used in source code for compilation or interpretation: identifiers do not need to be immediately resolved to an existing entity.
Examples
A basic example in C is:
void printThisInteger(int);
In
C and
C++, the line above represents a forward declaration of a
function and is the
function's prototype. After processing this declaration, the
compiler
In computing, a compiler is a computer program that translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primarily used for programs that ...
would allow the program code to refer to the entity
printThisInteger
in the rest of the program. The definition for a function must be provided somewhere (same file or other, where it would be the responsibility of the linker to correctly match references to a particular function in one or several object files with the definition, which must be unique, in another):
void printThisInteger(int x)
Variables may have only forward declaration and lack definition. During compilation time these are initialized by language specific rules (to undefined values, 0, NULL pointers, ...). Variables that are defined in other source/object files must have a forward declaration specified with a keyword
extern
:
int foo; //foo might be defined somewhere in this file
extern int bar; //bar must be defined in some other file
In
Pascal and other
Wirth programming languages, it is a general rule that all entities must be declared before use, and thus forward declaration is necessary for mutual recursion, for instance. In C, the same general rule applies, but with an exception for undeclared functions and incomplete types. Thus, in C it is possible (although unwise) to implement a pair of
mutually recursive functions thus:
int first(int x)
int second(int x)
In Pascal, the same implementation requires a forward declaration of
second
to precede its use in
first
. Without the forward declaration, the compiler will produce an error message indicating that the
identifier
An identifier is a name that identifies (that is, labels the identity of) either a unique object or a unique ''class'' of objects, where the "object" or class may be an idea, physical countable object (or class thereof), or physical noncountable ...
second
has been used without being declared.
Classes
In some object-oriented languages like
C++ and
Objective-C
Objective-C is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. Originally developed by Brad Cox and Tom Love in the early 1980s, it was selected by NeXT for its N ...
, it is sometimes necessary to forward-declare classes. This is done in situations when it is necessary to know that the name of the class is a type, but where it is unnecessary to know the structure.
In C++, classes and structs can be forward-declared like this:
class MyClass;
struct MyStruct;
In C++, classes can be forward-declared if you only need to use the pointer-to-that-class type (since all object pointers are the same size, and this is what the compiler cares about). This is especially useful inside class definitions, e.g. if a class contains a member that is a pointer (or a reference) to another class.
Forward-declaration is used to avoid unnecessary coupling which help reducing compilation time by reducing the number of header inclusion. This has a triple advantage:
* reduce the number of files opened by #include (hence the number of operating system calls)
* reducing the volume of the pre-processed files (as the header is not included)
* reducing recompilation impact when the forward declared class is modified.
Forward declaration of a class is not sufficient if you need to use the actual class type, for example, if you have a member whose type is that class directly (not a pointer), or if you need to use it as a base class, or if you need to use the methods of the class in a method.
In Objective-C, classes and protocols can be forward-declared like this:
@class MyClass;
@protocol MyProtocol;
In Objective-C, classes and protocols can be forward-declared if you only need to use them as part of an object pointer type, e.g. or . This is especially useful inside class definitions, e.g. if a class contains a member that is a pointer to another class; to avoid circular references (i.e. that class might also contain a member that is a pointer to this class), we simply forward-declare the classes instead.
Forward declaration of a class or protocol is not sufficient if you need to subclass that class or implement that protocol.
Forward reference
The term forward reference is sometimes used as a synonym of ''forward declaration''. However, more often it is taken to refer to the actual ''use'' of an entity before any declaration; that is, the first reference to
second
in the code above is a forward reference.
Thinking in C++: Inlines & the compiler
/ref> Thus, we may say that because forward declarations are mandatory in Pascal, forward ''references'' are prohibited.
An example of (valid) forward reference in C++:
class C {
public:
void mutator(int x) { myValue = x; }
int accessor() const { return myValue; }
private:
int myValue;
};
In this example, there are two references to myValue
before it is declared. C++ generally prohibits forward references, but they are allowed in the special case of class members. Since the member function accessor
cannot be compiled until the compiler knows the type of the member variable
In object-oriented programming, a member variable (sometimes called a member field) is a variable that is associated with a specific object, and accessible for all its methods (''member functions'').
In class-based programming languages, thes ...
myValue
, it is the compiler's responsibility to remember the definition of accessor
until it sees myValue
's declaration.
Permitting forward references can greatly increase the complexity and memory requirements of a compiler, and generally prevents the compiler from being implemented in one pass.
References
Programming constructs
Articles with example C code