C++C++11C++14C++17Introduction to C++Language FeatureLearn C++

What Are Generalized Constant Expressions (constexpr) In C++?

What Are Generalized Constant Expressions constexpr In C++

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++.

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:

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,

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:

Is there a full example of how to use generalized constant expressions in modern C++?

Here is a full example about constexpr;

What Are Generalized Constant Expressions constexpr In C++ The C++ Builder Logo

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.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?