Aussie AI
Constant Specifiers
-
Book Excerpt from "Generative AI in C++"
-
by David Spuler, Ph.D.
Constant Specifiers
The “const” keyword means that something is constant, and cannot be modified.
It is helpful for efficiency, but its role is also to help detect programming errors,
where code accidentally attempts to modify a constant variable or object.
There are multiple places where “const” can be used.
- Symbolic constants
constvariablesconstobjectsconstfunction parameters (i.e., “const&” idiom)constmember functions (read-only)
But don't get me started on “const correctness."
I've seen too many dawns fighting with compilers about const.
Anyway, let's move on, and assume we love const.
Basic const symbols.
Symbolic constants can be declared
as a representation of a numeric value or other type data (instead of using #define symbols):
const float pi = 3.14;
Set-once variables with const.
Variables can be made constant via “const”,
which is effectively the same as a symbolic constant,
except that the initializer need not be a compile-time constant.
It is a “set-only-once” variable.
The C++ compiler ensures that const variables cannot be modified,
once they are initialized.
const int scale_factor = get_config("scale");
const int primes[] = { 2, 3, 5, 7, 11, 13, 17 };
Function parameters and const.
The const specifier can ensure that function parameters
are not modified, especially for arrays passed by reference.
const on a scalar parameter type such as int is not as useful,
only ensuring that the code inside the function doesn't modify the parameter
(which isn't really a problem anyway).
However, the idiom of “const&” to specify a const reference
as a function parameter
allows constant pass-by-reference of object parameters, which
is extremely important for C++ efficiency.
Instantiate-only objects with const.
Class objects can be declared as const variables.
When the variable is a const object, it can be instantiated
via a constructor,
but cannot be modified thereafter.
const Complex cfactor(3.14, 1.0);
Member functions declared const.
Class member functions can be declared by adding the keyword “const” immediately
after the function parameter list:
int MyVector::count() const;
The C++ compiler blocks a const member function from modifying
data members, although it can still change “static” data members.
For const object variables, the C++ compiler
ensures that any calls to non-const member functions are disallowed.
Non-member functions.
Note that a non-member function cannot be const. The actions of a friend function
or other non-class function are controlled by using const on the parameters,
rather than the whole function itself.
Beyond const.
Newer C++ features have generalized and improved some of the uses of const.
The “constexpr” specifier is much more powerful in terms of allowing
compile-time optimizations,
as are its derivatives “constinit” and “consteval."
The newer use of “inline” on a variable (yes, a variable, not a function, supported since C++17), can be helpful for safely sharing constants
across multiple files.
|
• Next: • Up: Table of Contents |
|
The new AI programming book by Aussie AI co-founders:
Get your copy from Amazon: Generative AI in C++ |