HOME

TheInfoList



OR:

In some
programming language A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language. The description of a programming ...
s, const is a
type qualifier In the C, C++, and D programming languages, a type qualifier is a keyword that is applied to a type, resulting in a ''qualified type.'' For example, const int is a qualified type representing a constant integer, while int is the corresponding u ...
(a
keyword Keyword may refer to: Computing * Keyword (Internet search), a word or phrase typically used by bloggers or online content creator to rank a web page on a particular topic * Index term, a term used as a keyword to documents in an information syst ...
applied to a
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 ...
) that indicates that the data is read-only. While this can be used to declare constants, in the C family of languages differs from similar constructs in other languages in being part of the ''type,'' and thus has complicated behavior when combined with
pointers Pointer may refer to: Places * Pointer, Kentucky * Pointers, New Jersey * Pointers Airport, Wasco County, Oregon, United States * The Pointers, a pair of rocks off Antarctica People with the name * Pointer (surname), a surname (including a lis ...
, references,
composite data type In computer science, a composite data type or compound data type is any data type which can be constructed in a program using the programming language's primitive data types and other composite types. It is sometimes called a structure or aggreg ...
s, and
type-checking In computer programming, a type system is a logical system comprising a set of rules that assigns a property called a type to every "term" (a word, phrase, or other set of symbols). Usually the terms are various constructs of a computer progra ...
. In other languages, the data is not in a single
memory location In computing, a memory address is a reference to a specific memory location used at various levels by software and hardware. Memory addresses are fixed-length sequences of digits conventionally displayed and manipulated as unsigned integers. S ...
, but copied at
compile time In computer science, compile time (or compile-time) describes the time window during which a computer program is compiled. The term is used as an adjective to describe concepts related to the context of program compilation, as opposed to concept ...
on each use. Languages which utilize it include C,
C++ C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
, D,
JavaScript JavaScript (), often abbreviated as JS, is a programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2022, 98% of Website, websites use JavaScript on the Client (computing), client side ...
,
Julia Julia is usually a feminine given name. It is a Latinate feminine form of the name Julio and Julius. (For further details on etymology, see the Wiktionary entry "Julius".) The given name ''Julia'' had been in use throughout Late Antiquity (e.g ...
, and
Rust Rust is an iron oxide, a usually reddish-brown oxide formed by the reaction of iron and oxygen in the catalytic presence of water or air moisture. Rust consists of hydrous iron(III) oxides (Fe2O3·nH2O) and iron(III) oxide-hydroxide (FeO(OH ...
.


Introduction

When applied in an
object Object may refer to: General meanings * Object (philosophy), a thing, being, or concept ** Object (abstract), an object which does not exist at any particular time or place ** Physical object, an identifiable collection of matter * Goal, an ...
declaration Declaration may refer to: Arts, entertainment, and media Literature * ''Declaration'' (book), a self-published electronic pamphlet by Michael Hardt and Antonio Negri * ''The Declaration'' (novel), a 2008 children's novel by Gemma Malley Music ...
, it indicates that the object is a constant: its
value Value or values may refer to: Ethics and social * Value (ethics) wherein said concept may be construed as treating actions themselves as abstract objects, associating value to them ** Values (Western philosophy) expands the notion of value beyo ...
may not be changed, unlike a
variable Variable may refer to: * Variable (computer science), a symbolic name associated with a value and whose associated value may be changed * Variable (mathematics), a symbol that represents a quantity in a mathematical expression, as used in many ...
. This basic use – to declare constants – has parallels in many other languages. However, unlike in other languages, in the C family of languages the const is part of the ''type'', not part of the ''object''. For example, in C, declares an object x of int const type – the const is part of the type, as if it were parsed "(int const) x" – while in
Ada Ada may refer to: Places Africa * Ada Foah, a town in Ghana * Ada (Ghana parliament constituency) * Ada, Osun, a town in Nigeria Asia * Ada, Urmia, a village in West Azerbaijan Province, Iran * Ada, Karaman, a village in Karaman Province, ...
, declares a constant (a kind of object) X of INTEGER type: the constant is part of the ''object'', but not part of the ''type''. This has two subtle results. Firstly, const can be applied to parts of a more complex type – for example, int const * const x; declares a constant pointer to a constant integer, while int const * x; declares a variable pointer to a constant integer, and int * const x; declares a constant pointer to a variable integer. Secondly, because const is part of the type, it must match as part of type-checking. For example, the following code is invalid: void f(int& x); // ... int const i; f(i); because the argument to f must be a ''variable'' integer, but i is a ''constant'' integer. This matching is a form of
program correctness In theoretical computer science, an algorithm is correct with respect to a specification if it behaves as specified. Best explored is ''functional'' correctness, which refers to the input-output behavior of the algorithm (i.e., for each input it p ...
, and is known as const-correctness. This allows a form of programming by contract, where functions specify as part of their
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 typ ...
whether they modify their arguments or not, and whether their
return value In computer programming, a return statement causes execution to leave the current subroutine and resume at the point in the code immediately after the instruction which called the subroutine, known as its return address. The return address is sa ...
is modifiable or not. This type-checking is primarily of interest in pointers and references – not basic value types like integers – but also for
composite data type In computer science, a composite data type or compound data type is any data type which can be constructed in a program using the programming language's primitive data types and other composite types. It is sometimes called a structure or aggreg ...
s or templated types such as
containers A container is any receptacle or enclosure for holding a product used in storage, packaging, and transportation, including shipping. Things kept inside of a container are protected on several sides by being inside of its structure. The term ...
. It is concealed by the fact that the const can often be omitted, due to
type coercion In computer science, type conversion, type casting, type coercion, and type juggling are different ways of changing an expression from one data type to another. An example would be the conversion of an integer value into a floating point valu ...
(implicit
type conversion In computer science, type conversion, type casting, type coercion, and type juggling are different ways of changing an expression from one data type to another. An example would be the conversion of an integer value into a floating point value ...
) and C being
call-by-value In a programming language, an evaluation strategy is a set of rules for evaluating expressions. The term is often used to refer to the more specific notion of a ''parameter-passing strategy'' that defines the kind of value that is passed to the f ...
(C++ and D are either call-by-value or call-by-reference).


Consequences

The idea of const-ness does not imply that the variable as it is stored in
computer memory In computing, memory is a device or system that is used to store information for immediate use in a computer or related computer hardware and digital electronic devices. The term ''memory'' is often synonymous with the term ''primary storage ...
is unwritable. Rather, const-ness is a
compile-time In computer science, compile time (or compile-time) describes the time window during which a computer program is compiled. The term is used as an adjective to describe concepts related to the context of program compilation, as opposed to concept ...
construct that indicates what a programmer ''should'' do, not necessarily what they ''can'' do. Note, however, that in the case of predefined data (such as char const *
string literal A string literal or anonymous string is a string value in the source code of a computer program. Modern programming languages commonly use a quoted sequence of characters, formally " bracketed delimiters", as in x = "foo", where "foo" is a string ...
s), C const is ''often'' unwritable.


Distinction from constants

While a constant does not change its value while the program is running, an object declared const may indeed change its value while the program is running. A common example are read only registers within embedded systems like the current state of a digital input. The data registers for digital inputs are often declared as const and volatile. The content of these registers may change without the program doing anything (volatile) but you shall not write to them either (const).


Other uses

In addition, a (non-static) member-function can be declared as const. In this case, the this pointer inside such a function is of type object_type const * rather than merely of type object_type *. This means that non-const functions for this object cannot be called from inside such a function, nor can member variables be modified. In C++, a member variable can be declared as
mutable In object-oriented and functional programming, an immutable object (unchangeable object) is an object whose state cannot be modified after it is created.Goetz et al. ''Java Concurrency in Practice''. Addison Wesley Professional, 2006, Section 3 ...
, indicating that this restriction does not apply to it. In some cases, this can be useful, for example with caching, reference counting, and
data synchronization Data synchronization is the process of establishing consistency between source and target data stores, and the continuous harmonization of the data over time. It is fundamental to a wide variety of applications, including file synchronization and m ...
. In these cases, the logical meaning (state) of the object is unchanged, but the object is not physically constant since its bitwise representation may change.


Syntax

In C, C++, and D, all data types, including those defined by the user, can be declared const, and const-correctness dictates that all variables or objects should be declared as such unless they need to be modified. Such proactive use of const makes values "easier to understand, track, and reason about," and it thus increases the readability and comprehensibility of code and makes working in teams and maintaining code simpler because it communicates information about a value's intended use. This can help 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 ...
as well as the developer when reasoning about code. It can also enable an
optimizing compiler In computing, an optimizing compiler is a compiler that tries to minimize or maximize some attributes of an executable computer program. Common requirements are to minimize a program's execution time, memory footprint, storage size, and power cons ...
to generate more efficient code.


Simple data types

For simple non-pointer data types, applying the const qualifier is straightforward. It can go on either side of some types for historical reasons (for example, const char foo = 'a'; is equivalent to char const foo = 'a';). On some implementations, using const twice (for instance, const char const or char const const) generates a warning but not an error.


Pointers and references

For pointer and reference types, the meaning of const is more complicated – either the pointer itself, or the value being pointed to, or both, can be const. Further, the syntax can be confusing. A pointer can be declared as a const pointer to writable value, or a writable pointer to a const value, or const pointer to const value. A const pointer cannot be reassigned to point to a different object from the one it is initially assigned, but it can be used to modify the value that it points to (called the '' pointee''). Reference variables in C++ are an alternate syntax for const pointers. A pointer to a const object, on the other hand, can be reassigned to point to another memory location (which should be an object of the same type or of a convertible type), but it cannot be used to modify the memory that it is pointing to. A const pointer to a const object can also be declared and can neither be used to modify the pointee nor be reassigned to point to another object. The following code illustrates these subtleties: void Foo( int * ptr, int const * ptrToConst, int * const constPtr, int const * const constPtrToConst )


C convention

Following usual C convention for declarations, declaration follows use, and the * in a pointer is written on the pointer, indicating
dereferencing In computer programming, the dereference operator or indirection operator, sometimes denoted by "*" (i.e. an asterisk), is a unary operator (i.e. one with a single operand) found in List of C-family programming languages, C-like languages that in ...
. For example, in the declaration int *ptr, the dereferenced form *ptr is an int, while the reference form ptr is a pointer to an int. Thus const modifies the ''name'' to its right. The C++ convention is instead to associate the * with the type, as in int* ptr, and read the const as modifying the ''type'' to the left. int const * ptrToConst can thus be read as "*ptrToConst is a int const" (the value is constant), or "ptrToConst is a int const *" (the pointer is a pointer to a constant integer). Thus: int *ptr; // *ptr is an int value int const *ptrToConst; // *ptrToConst is a constant (int: integer value) int * const constPtr; // constPtr is a constant (int *: integer pointer) int const * const constPtrToConst; // constPtrToConst is a constant pointer and points // to a constant value


C++ convention

Following C++ convention of analyzing the type, not the value, a
rule of thumb In English, the phrase ''rule of thumb'' refers to an approximate method for doing something, based on practical experience rather than theory. This usage of the phrase can be traced back to the 17th century and has been associated with various t ...
is to read the declaration from right to left. Thus, everything to the left of the star can be identified as the pointee type and everything to the right of the star are the pointer properties. For instance, in our example above, int const * can be read as a writable pointer that refers to a non-writable integer, and int * const can be read as a non-writable pointer that refers to a writable integer. A more generic rule that helps you understand complex declarations and definitions works like this: # find the identifier whose declaration you want to understand # read as far as possible to the right (i.e., until the end of the declaration or to the next closing parenthesis, whichever comes first) # back up to where you began, and read backwards to the left (i.e., until the beginning of the declaration or to the open-parenthesis matching the closing parenthesis found in the previous step) # when you've reached the beginning of the declaration you're done. If not, continue at step 2, beyond the closing parenthesis that was matched last. Here is an example: When reading to the left, it is important that you read the elements from right to left. So an int const * becomes a ''pointer to a const int'' and not a ''const pointer to an int''. In some cases C/C++ allows the const keyword to be placed to the left of the type. Here are some examples: const int *ptrToConst; //identical to: int const *ptrToConst, const int *const constPtrToConst; //identical to: int const *const constPtrToConst Although C/C++ allows such definitions (which closely match the English language when reading the definitions from left to right), the compiler still reads the definitions according to the abovementioned procedure: from right to left. But putting const ''before'' what must be constant quickly introduces mismatches between what you intend to write and what the compiler decides you wrote. Consider pointers to pointers: int **ptr; // a pointer to a pointer to ints int const **ptr // a pointer to a pointer to constant int value // (not a pointer to a constant pointer to ints) int *const *ptr // a pointer to a const pointer to int values // (not a constant pointer to a pointer to ints) int **const ptr // a constant pointer to pointers to ints // (ptr, the identifier, being const makes no sense) int const **const ptr // a constant pointer to pointers to constant int values As a final note regarding pointer definitions: always write the pointer symbol (the *) as much as possible to the right. Attaching the pointer symbol to the type is tricky, as it strongly suggests a pointer type, which isn't the case. Here are some examples: int* a; /* write: */ int *a; // a is a pointer to an int int* a, b; // CONFUSING /* write: */ int *a, b; // a is a pointer to an int, // but b is a mere int int* a, *b; // UGLY: both a and b are pointers to ints /* write: */ int *a, *b; Bjarne Stroustrup's FAQ recommends only declaring one variable per line if using the C++ convention, to avoid this issue. The same considerations apply to defining references and rvalue references: int var = 22; int const &refToConst = var; // OK int const& ref2 = var, ref3 = var; // CONFUSING: // ref2 is a reference, but ref3 isn't: // ref3 is a constant int initialized with // var's value int &const constRef = var; // ERROR: as references can't change anyway. // C++: int&& rref = int(5), value = 10; // CONFUSING: // rref is an rvalue reference, but value is // a mere int. /* write: */ int &&rref = int(5), value = 10; More complicated declarations are encountered when using multidimensional arrays and references (or pointers) to pointers. Although it is sometimes argued that such declarations are confusing and error-prone and that they therefore should be avoided or be replaced by higher-level structures, the procedure described at the top of this section can always be used without introducing ambiguities or confusion.


Parameters and variables

const can be declared both on function parameters and on variables (
static Static may refer to: Places *Static Nunatak, a nunatak in Antarctica United States * Static, Kentucky and Tennessee *Static Peak, a mountain in Wyoming **Static Peak Divide, a mountain pass near the peak Science and technology Physics *Static el ...
or automatic, including global or local). The interpretation varies between uses. A const static variable (global variable or static local variable) is a constant, and may be used for data like mathematical constants, such as double const PI = 3.14159 – realistically longer, or overall compile-time parameters. A const automatic variable (non-static local variable) means that
single assignment In computer programming, an assignment statement sets and/or re-sets the value stored in the storage location(s) denoted by a variable name; in other words, it copies a value into the variable. In most imperative programming languages, the as ...
is happening, though a different value may be used each time, such as int const x_squared = x * x. A const parameter in pass-by-reference means that the referenced value is not modified – it is part of the
contract A contract is a legally enforceable agreement between two or more parties that creates, defines, and governs mutual rights and obligations between them. A contract typically involves the transfer of goods, services, money, or a promise to tran ...
– while a const parameter in pass-by-value (or the pointer itself, in pass-by-reference) does not add anything to the interface (as the value has been copied), but indicates that internally, the function does not modify the local copy of the parameter (it is a single assignment). For this reason, some favor using const in parameters only for pass-by-reference, where it changes the contract, but not for pass-by-value, where it exposes the implementation.


C++


Methods

In order to take advantage of the design by contract approach for user-defined types (structs and classes), which can have methods as well as member data, the programmer may tag instance methods as const if they don't modify the object's data members. Applying the const qualifier to instance methods thus is an essential feature for const-correctness, and is not available in many other
object-oriented Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of pro ...
languages such as
Java Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's List ...
and C# or in
Microsoft Microsoft Corporation is an American multinational technology corporation producing computer software, consumer electronics, personal computers, and related services headquartered at the Microsoft Redmond campus located in Redmond, Washing ...
's
C++/CLI C++/CLI is a variant of the C++ programming language, modified for Common Language Infrastructure. It has been part of Visual Studio 2005 and later, and provides interoperability with other .NET languages such as C#. Microsoft created C++/CLI ...
or Managed Extensions for C++. While const methods can be called by const and non-const objects alike, non-const methods can only be invoked by non-const objects. The const modifier on an instance method applies to the object pointed to by the "
this This may refer to: * ''This'', the singular proximal demonstrative pronoun Places * This, or ''Thinis'', an ancient city in Upper Egypt * This, Ardennes, a commune in France People with the surname * Hervé This, French culinary chemist Arts, ...
" pointer, which is an implicit argument passed to all instance methods. Thus having const methods is a way to apply const-correctness to the implicit "this" pointer argument just like other arguments. This example illustrates: class C ; void Foo(C& nonConstC, C const& constC) In the above code, the implicit "this" pointer to Set() has the type "C *const"; whereas the "this" pointer to Get() has type "C const *const", indicating that the method cannot modify its object through the "this" pointer. Often the programmer will supply both a const and a non-const method with the same name (but possibly quite different uses) in a class to accommodate both types of callers. Consider: class MyArray ; void Foo( MyArray & array, MyArray const & constArray ) The const-ness of the calling object determines which version of MyArray::Get() will be invoked and thus whether or not the caller is given a reference with which he can manipulate or only observe the private data in the object. The two methods technically have different signatures because their "this" pointers have different types, allowing the compiler to choose the right one. (Returning a const reference to an int, instead of merely returning the int by value, may be overkill in the second method, but the same technique can be used for arbitrary types, as in the
Standard Template Library The Standard Template Library (STL) is a Library (computer science), software library originally designed by Alexander Stepanov for the C++ programming language that influenced many parts of the C++ Standard Library. It provides four components ...
.)


Loopholes to const-correctness

There are several loopholes to pure const-correctness in C and C++. They exist primarily for compatibility with existing code. The first, which applies only to C++, is the use of const_cast, which allows the programmer to strip the const qualifier, making any object modifiable. The necessity of stripping the qualifier arises when using existing code and libraries that cannot be modified but which are not const-correct. For instance, consider this code: // Prototype for a function which we cannot change but which // we know does not modify the pointee passed in. void LibraryFunc(int* ptr, int size); void CallLibraryFunc(int const * ptr, int size) However, any attempt to modify an object that is itself declared const by means of a
const cast In computer science, type conversion, type casting, type coercion, and type juggling are different ways of changing an expression from one data type to another. An example would be the conversion of an integer value into a floating point valu ...
results in undefined behavior according to the ISO C++ Standard. In the example above, if ptr references a global, local, or member variable declared as const, or an object allocated on the heap via new int const, the code is only correct if LibraryFunc really does not modify the value pointed to by ptr. The C language has a need of a loophole because a certain situation exists. Variables with static storage duration are allowed to be defined with an initial value. However, the initializer can use only constants like string constants and other literals, and is not allowed to use non-constant elements like variable names, whether the initializer elements are declared const or not, or whether the static duration variable is being declared const or not. There is a non-portable way to initialize a const variable that has static storage duration. By carefully constructing a typecast on the left hand side of a later assignment, a const variable can be written to, effectively stripping away the const attribute and 'initializing' it with non-constant elements like other const variables and such. Writing into a const variable this way may work as intended, but it causes undefined behavior and seriously contradicts const-correctness: size_t const bufferSize = 8*1024; size_t const userTextBufferSize; //initial value depends on const bufferSize, can't be initialized here ... int setupUserTextBox(textBox_t *defaultTextBoxType, rect_t *defaultTextBoxLocation) Another loophole applies both to C and C++. Specifically, the languages dictate that member pointers and references are "shallow" with respect to the const-ness of their owners — that is, a containing object that is const has all const members except that member pointees (and referees) are still mutable. To illustrate, consider this C++ code: struct S ; void Foo(S const & s) Although the object s passed to Foo() is constant, which makes all of its members constant, the pointee accessible through s.ptr is still modifiable, though this may not be desirable from the standpoint of const-correctness because s might solely own the pointee. For this reason, Meyers argues that the default for member pointers and references should be "deep" const-ness, which could be overridden by a mutable qualifier when the pointee is not owned by the container, but this strategy would create compatibility issues with existing code. Thus, for historical reasons, this loophole remains open in C and C++. The latter loophole can be closed by using a class to hide the pointer behind a const-correct interface, but such classes either do not support the usual copy semantics from a const object (implying that the containing class cannot be copied by the usual semantics either) or allow other loopholes by permitting the stripping of const-ness through inadvertent or intentional copying. Finally, several functions in the C standard library violate const-correctness, as they accept a const pointer to a character string and return a non-const pointer to a part of the same string.
strtol The C (programming language), C programming language has a set of functions implementing operations on String (computer science), strings (character strings and byte strings) in its C standard library, standard library. Various operations, such ...
and
strchr The C (programming language), C programming language has a set of functions implementing operations on String (computer science), strings (character strings and byte strings) in its C standard library, standard library. Various operations, such ...
are among these functions. Some implementations of the C++ standard library, such as Microsoft's try to close this loophole by providing two overloaded versions of some functions: a "const" version and a "non-const" version.


Problems

The use of the type system to express constancy leads to various complexities and problems, and has accordingly been criticized and not adopted outside the narrow C family of C, C++, and D. Java and C#, which are heavily influenced by C and C++, both explicitly rejected const-style type qualifiers, instead expressing constancy by keywords that apply to the identifier (final in Java, const and readonly in C#). Even within C and C++, the use of const varies significantly, with some projects and organizations using it consistently, and others avoiding it.


strchr problem

The const type qualifier causes difficulties when the logic of a function is agnostic to whether its input is constant or not, but returns a value which should be of the same qualified type as an input. In other words, for these functions, if the input is constant (const-qualified), the return value should be as well, but if the input is variable (not const-qualified), the return value should be as well. Because the
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 typ ...
of these functions differs, it requires two functions (or potentially more, in case of multiple inputs) with the same logic – a form of
generic programming Generic programming is a style of computer programming in which algorithms are written in terms of types ''to-be-specified-later'' that are then ''instantiated'' when needed for specific types provided as parameters. This approach, pioneered b ...
. This problem arises even for simple functions in the C standard library, notably strchr; this observation is credited by Ritchie to Tom Plum in the mid 1980s. The strchr function locates a character in a string; formally, it returns a pointer to the first occurrence of the character c in the string s, and in classic C (K&R C) its prototype is: char *strchr(char *s, int c); The strchr function does not modify the input string, but the return value is often used by the caller to modify the string, such as: if (p = strchr(q, '/')) *p = ' '; Thus on the one hand the input string ''can'' be const (since it is not modified by the function), and if the input string is const the return value should be as well – most simply because it might return exactly the input pointer, if the first character is a match – but on the other hand the return value should not be const if the original string was not const, since the caller may wish to use the pointer to modify the original string. In C++ this is done via
function overloading In some programming languages, function overloading or method overloading is the ability to create multiple functions of the same name with different implementations. Calls to an overloaded function will run a specific implementation of that f ...
, typically implemented via a
template Template may refer to: Tools * Die (manufacturing), used to cut or shape material * Mold, in a molding process * Stencil, a pattern or overlay used in graphic arts (drawing, painting, etc.) and sewing to replicate letters, shapes or designs ...
, resulting in two functions, so that the return value has the same const-qualified type as the input: char* strchr(char* s, int c); char const* strchr(char const* s, int c); These can in turn be defined by a template: template T* strchr(T* s, int c) In D this is handled via the inout keyword, which acts as a wildcard for const, immutable, or unqualified (variable), yielding:''The D Programming Language,''
Andrei Alexandrescu Andrei Alexandrescu (born 1969) is a Romanian-American C++ and D language programmer and author. He is particularly known for his pioneering work on policy-based design implemented via template metaprogramming. These ideas are articulated ...
, 8.8: Propagating a Qualifier from Parameter to Result
inout(char)* strchr(inout(char)* s, int c); However, in C neither of these is possible since C does not have function overloading, and instead, this is handled by having a single function where the input is constant but the output is writable: char *strchr(char const *s, int c); This allows idiomatic C code but does strip the const qualifier if the input actually was const-qualified, violating type safety. This solution was proposed by Ritchie and subsequently adopted. This difference is one of the failures of
compatibility of C and C++ The C and C++ programming languages are closely related but have many significant differences. C++ began as a fork of an early, pre-standardized C, and was designed to be mostly source-and-link compatible with C compilers of the time. Due to thi ...
.


D

In Version 2 of the , two keywords relating to const exist. The immutable keyword denotes data that cannot be modified through any reference. The const keyword denotes a non-mutable view of mutable data. Unlike C++ const, D const and immutable are "deep" or , and anything reachable through a const or immutable object is const or immutable respectively. Example of const vs. immutable in D int[] foo = new int[5]; // foo is mutable. const int[] bar = foo; // bar is a const view of mutable data. immutable int[] baz = foo; // Error: all views of immutable data must be immutable. immutable int[] nums = new immutable(int)[5]; // No mutable reference to nums may be created. const int[] constNums = nums; // Works. immutable is implicitly convertible to const. int[] mutableNums = nums; // Error: Cannot create a mutable view of immutable data. Example of transitive or deep const in D class Foo immutable Foo foo = new immutable(Foo); foo.next.num = 5; // Won't compile. foo.next is of type immutable(Foo). // foo.next.num is of type immutable(int).


History

const was introduced by
Bjarne Stroustrup Bjarne Stroustrup (; ; born 30 December 1950) is a Danish computer scientist, most notable for the invention and development of the C++ programming language. As of July 2022, Stroustrup is a professor of Computer Science at Columbia University ...
in
C with Classes C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
, the predecessor to
C++ C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
, in 1981, and was originally called readonly.Sibling Rivalry: C and C++
Bjarne Stroustrup Bjarne Stroustrup (; ; born 30 December 1950) is a Danish computer scientist, most notable for the invention and development of the C++ programming language. As of July 2022, Stroustrup is a professor of Computer Science at Columbia University ...
, 2002, p. 5
As to motivation, Stroustrup writes: :"It served two functions: as a way of defining a symbolic constant that obeys scope and type rules (that is, without using a macro) and as a way of deeming an object in memory immutable." The first use, as a scoped and typed alternative to macros, was analogously fulfilled for function-like macros via the inline keyword. Constant pointers, and the * const notation, were suggested by Dennis Ritchie and so adopted. const was then adopted in C as part of standardization, and appears in C89 (and subsequent versions) along with the other type qualifier, volatile. A further qualifier, noalias, was suggested at the December 1987 meeting of the X3J11 committee, but was rejected; its goal was ultimately fulfilled by the
restrict In the C programming language, restrict is a keyword, introduced by the C99 standard, that can be used in pointer declarations. By adding this type qualifier, a programmer hints to the compiler that for the lifetime of the pointer, no other p ...
keyword in
C99 C99 (previously known as C9X) is an informal name for ISO/IEC 9899:1999, a past version of the C programming language standard. It extends the previous version ( C90) with new features for the language and the standard library, and helps impl ...
. Ritchie was not very supportive of these additions, arguing that they did not "carry their weight", but ultimately did not argue for their removal from the standard. D subsequently inherited const from C++, where it is known as a ''type constructor'' (not
type qualifier In the C, C++, and D programming languages, a type qualifier is a keyword that is applied to a type, resulting in a ''qualified type.'' For example, const int is a qualified type representing a constant integer, while int is the corresponding u ...
) and added two further type constructors, immutable and inout, to handle related use cases.


Other languages

Other languages do not follow C/C++ in having constancy part of the type, though they often have superficially similar constructs and may use the const keyword. Typically this is only used for constants (constant objects). C# has a const keyword, but with radically different and simpler semantics: it means a compile-time constant, and is not part of the type. Nim has a const keyword similar to that of C#: it also declares a compile-time constant rather than forming part of the type. However, in Nim, a constant can be declared from any expression that can be evaluated at compile time. In C#, only C# built-in types can be declared as const; user-defined types, including classes, structs, and arrays, cannot be const. Java does not have const – it instead has final, which can be applied to local "variable" declarations and applies to the ''identifier,'' not the type. It has a different object-oriented use for object members, which is the origin of the name. The Java language specification regards const as a reserved keyword – i.e., one that cannot be used as variable identifier – but assigns no semantics to it: it is a ''reserved word'' (it cannot be used in identifiers) but not a ''keyword'' (it has no special meaning). It is thought that the reservation of the keyword occurred to allow for an extension of the Java language to include C++-style const methods and pointer to const type. An enhancement request ticket for implementing const correctness exists in the
Java Community Process The Java Community Process (JCP), established in 1998, is a formalized mechanism that allows interested parties to develop standard technical specifications for Java technology. Anyone can become a JCP Member by filling a form available at thJCP w ...
, but was closed in 2005 on the basis that it was impossible to implement in a backwards-compatible fashion. The contemporary
Ada 83 Ada is a structured, statically typed, imperative, and object-oriented high-level programming language, extended from Pascal and other languages. It has built-in language support for '' design by contract'' (DbC), extremely strong typing, exp ...
independently had the notion of a constant object and a constant keyword, with
input parameter In computer programming, a parameter or a formal argument is a special kind of variable used in a subroutine to refer to one of the pieces of data provided as input to the subroutine. These pieces of data are the values of the arguments (often c ...
s and loop parameters being implicitly constant. Here the constant is a property of the object, not of the type.
JavaScript JavaScript (), often abbreviated as JS, is a programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2022, 98% of Website, websites use JavaScript on the Client (computing), client side ...
has a const declaration that defines a block-scoped variable that cannot be reassigned nor redeclared. It defines a read-only reference to a variable that cannot be redefined, but in some situations the value of the variable itself may potentially change, such as if the variable refers to an object and a property of it is altered.


See also

*
Single assignment In computer programming, an assignment statement sets and/or re-sets the value stored in the storage location(s) denoted by a variable name; in other words, it copies a value into the variable. In most imperative programming languages, the as ...
*
restrict In the C programming language, restrict is a keyword, introduced by the C99 standard, that can be used in pointer declarations. By adding this type qualifier, a programmer hints to the compiler that for the lifetime of the pointer, no other p ...
*
Pointer aliasing In computing, aliasing describes a situation in which a data location in memory can be accessed through different symbolic names in the program. Thus, modifying the data through one name implicitly modifies the values associated with all aliased n ...


Notes


References


External links


"Const-Correctness"
by
Herb Sutter Herb Sutter is a prominent C++ expert. He is also a book author and was a columnist for Dr. Dobb's Journal. He joined Microsoft in 2002 as a platform evangelist for Visual C++ .NET, rising to lead software architect for C++/CLI. Sutter has ser ...

"Constant Optimization?"
by Herb Sutter

by Marshall Cline * Section

from the free electronic book '' Thinking in C++'' by
Bruce Eckel Bruce Eckel (born July 8, 1957) is a computer programmer, author and consultant. His best known works are ''Thinking in Java'' and ''Thinking in C++'', aimed at programmers wanting to learn the Java or C++ programming languages, particularly those ...

"Here A Const, There A Const"
by
Walter Bright Walter G. Bright is an American computer programmer who created the D programming language, the Zortech C++ compiler, and the ''Empire'' computer game. Early life and education Bright is the son of the United States Air Force pilot Charles D. ...

"Const and Invariant" from D programming language specification, version 2 (experimental)
{{DEFAULTSORT:Const-Correctness C programming language family Data types Articles with example C code Articles with example C++ code Programming language topics