Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Understanding constexpr | Type Inference and Initialization
Quizzes & Challenges
Quizzes
Challenges
/
C++ Modern Features

bookUnderstanding constexpr

The constexpr keyword in C++ allows you to declare variables, functions, and even constructors whose values or return results are guaranteed to be evaluated at compile time. This means the computation happens during compilation, not at runtime, enabling more efficient and predictable code. When you mark an expression or function as constexpr, you signal to the compiler that its result can be determined before the program runs, as long as all inputs are also available at compile time.

While const simply marks a variable as read-only after initialization, it does not guarantee that the value is known during compilation. In contrast, constexpr enforces that the value must be computable at compile time, providing stronger guarantees and unlocking optimizations such as using these values in array sizes, template parameters, and other compile-time contexts. By leveraging constexpr, you write code that is both safer and potentially faster, as errors can be caught earlier and runtime overhead is reduced.

main.cpp

main.cpp

copy
12345678910111213141516171819
#include <iostream> // `constexpr` function computes factorial at compile time constexpr int factorial(int n) { return (n <= 1) ? 1 : (n * factorial(n - 1)); } int main() { // constexpr variable: value computed at compile time constexpr int five_factorial = factorial(5); std::cout << "5! = " << five_factorial << '\n'; // Using constexpr value in array size (must be known at compile time) int arr[five_factorial]; std::cout << "Array of size " << sizeof(arr)/sizeof(arr[0]) << " created.\n"; }

Using constexpr ensures that expressions are evaluated during compilation, which catches errors early and prevents invalid code from compiling. This compile-time evaluation can improve performance by eliminating runtime calculations and enabling better optimization by the compiler. However, all constexpr expressions must be fully computable at compile time; if an expression depends on values only known at runtime, it cannot be marked as constexpr. This restriction means you must design constexpr functions and variables to avoid side effects and rely only on other compile-time constants or parameters.

question mark

What is the main purpose of the constexpr keyword in C++?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 3

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Awesome!

Completion rate improved to 14.29

bookUnderstanding constexpr

Свайпніть щоб показати меню

The constexpr keyword in C++ allows you to declare variables, functions, and even constructors whose values or return results are guaranteed to be evaluated at compile time. This means the computation happens during compilation, not at runtime, enabling more efficient and predictable code. When you mark an expression or function as constexpr, you signal to the compiler that its result can be determined before the program runs, as long as all inputs are also available at compile time.

While const simply marks a variable as read-only after initialization, it does not guarantee that the value is known during compilation. In contrast, constexpr enforces that the value must be computable at compile time, providing stronger guarantees and unlocking optimizations such as using these values in array sizes, template parameters, and other compile-time contexts. By leveraging constexpr, you write code that is both safer and potentially faster, as errors can be caught earlier and runtime overhead is reduced.

main.cpp

main.cpp

copy
12345678910111213141516171819
#include <iostream> // `constexpr` function computes factorial at compile time constexpr int factorial(int n) { return (n <= 1) ? 1 : (n * factorial(n - 1)); } int main() { // constexpr variable: value computed at compile time constexpr int five_factorial = factorial(5); std::cout << "5! = " << five_factorial << '\n'; // Using constexpr value in array size (must be known at compile time) int arr[five_factorial]; std::cout << "Array of size " << sizeof(arr)/sizeof(arr[0]) << " created.\n"; }

Using constexpr ensures that expressions are evaluated during compilation, which catches errors early and prevents invalid code from compiling. This compile-time evaluation can improve performance by eliminating runtime calculations and enabling better optimization by the compiler. However, all constexpr expressions must be fully computable at compile time; if an expression depends on values only known at runtime, it cannot be marked as constexpr. This restriction means you must design constexpr functions and variables to avoid side effects and rely only on other compile-time constants or parameters.

question mark

What is the main purpose of the constexpr keyword in C++?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 3
some-alt