C++ is really awesome programming language that has many options to use variables in a way which can make your C++ App use less memory and app size. In this post, we explain what are a constant, a constant expression and generalized constant expressions in modern C++.
Table of Contents
What is the const constant keyword in modern C++?
Let’s remember that, in C++ there are two ways to define constants. We can use #define
preprocessors and we can use the const
keyword before the variable type. To do this, we use the const
prefix to declare constant variables with their specific types. The main difference between #define
and const
is that const
keywords can be used inside functions. Here is a very simple example of how to use the const prefix:
1 |
const float GRAVITY = 9.81; |
This can be applied to any types int
, float
, double
, etc. If you want to know more about constants in C++, please check our post about constants and literals below.
Now, let see what is a generalized constant expression in modern C++?
What are constexpr (generalized constant expressions) in modern C++?
A Constant Expression (constexpr) defines an expression that the value of a variable or function can be used in constant expressions that are evaluated at compile time. The C++11 standard generalizes the concept of constant expressions with a new keyword constexpr
as a declaration specifier.
Here are two different constexpr
examples in use,
1 2 3 |
constexpr double d1 = 7.0/8.0; constexpr int fx(); // fx() is declared before |
Generalized Constant Expression is a constant expression that comes with the C++11 standard and generalizes the notion of constant expressions to include calls to simple functions constexpr
and objects of user-defined types that are constructed from simple constructors (constexpr constructors).
A function is a constant-expression function if:
- It returns a value (void type not allowed).
- The function body consists of a single statement, the return statement:
return expr;
- It is declared with the keyword
constexpr
.
A variable or a data member is a constant-expression value if:
- It is declared with the keyword
constexpr
. - It is initialized with a constant expression or an rvalue constructed by a constant-expression constructor with constant-expression arguments.
A constructor is a constant-expression constructor if:
- It is declared with the keyword
constexpr
. - The member-initializer part involves only potential constant expressions.
- Its body is empty.
Is there a simple example of using generalized constant expressions in modern C++?
The following example contains a constexpr
function, a constexpr
variable, and a constexpr
constructor:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
constexpr int add(int x, int y) { return x + y; } constexpr double var = 2 * add(3, 4); class A { constexpr A(int newA, int newB) : a(newA), b(newB) { } private: int a; int b; }; |
Is there a full example of how to use generalized constant expressions in modern C++?
Here is a full example about constexpr
;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <iostream> constexpr int add(int x, int y) { return x + y; } constexpr double var = 2 * add(3, 4); class A { constexpr A(int newA, int newB) : a(newA), b(newB) { } private: int a; int b; }; int main() { return 0; } |
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.