Home C++ Decisions Loops Input/Output Functions Stack and Heap References Arrays Searching and Sorting Recursion Pointers Character and Strings Structures Classes Inheritance Exceptions Templatess STL Modern C++ Misc Books ----

constexpr


Contents

Introduction

This is similar to the "const" and "inline" features. The "constexpr" is usually evaluated at compile time. The "const" may be evaluated at run time and a variable can be assigned to it. The "inline" may substitute the function call by the body of the function but the "constexpr" actually evaluates the whole function and places the result at compile time if it can.
File: constexpr1.cpp
#include <iostream>


int main()
{
    int runtime_value = 10;

    const int const_runtime_init = runtime_value;
    // OK, const_runtime_init is a const int initialized at runtime
    // constexpr int constexpr_runtime_init = runtime_value;
    // ERROR: runtime_value is not a constant expression

    const int const_compile_time_init = 5;
    // OK, const int initialized with a compile-time constant
    constexpr int constexpr_compile_time_init = 5;
    // OK, constexpr int initialized with a compile-time constant

    int arr1[const_compile_time_init];
    // OK, const_compile_time_init is a compile-time constant
    int arr2[constexpr_compile_time_init];
    // OK, constexpr_compile_time_init is a compile-time constant

    // int arr3[const_runtime_init];
    // ERROR: const_runtime_init might not be a compile-time constant

      constexpr int array_size = 10  ;

      // array_size is a compile-time constant
      int arr[array_size];


  return 0 ;
}
The "constexpr" when applied to functions will try to evaluate it at compile if it can. Otherwise it will be evaluated at run time.

File: constexpr2.cpp
#include <iostream>

// A constexpr function to calculate the product of two integers
constexpr int product(int x, int y)
{
    return x * y;
}

int main()
{
    // Calling product with constant arguments, allowing compile-time evaluation
    constexpr int compileTimeResult = product(5, 7);
    cout << "Compile-time result: " << compileTimeResult << endl; // Output: 35

    // Using the constexpr function to define the size of a C-style array
    int arr[product(2, 3)]; // Size of arr is 6, determined at compile time

    // Initialize and print elements of the array
    for (int i = 0; i < product(2, 3); ++i) {
        arr[i] = i + 1;
        cout << arr[i] << " ";
    }
    cout << endl; // Output: 1 2 3 4 5 6

    // Calling product with a runtime argument, forcing runtime evaluation
    int a = 10;
    int runtimeResult = product(a, 4); // Evaluated at runtime
    cout << "Runtime result: " << runtimeResult << endl; // Output: 40

    return 0;
}

Exercise

1) Plug the below code in https://cppinsights.io/ to see the warning. Convert the function to use "constexpr" and get rid of the warning. Observe the constant for the array dimension.

File: const_ex1.cpp
#include <iostream>


int sum( int x1, int y1 )
{
    return x1 * y1 ;
}

int main()
{

    int arr[  sum(3, 3) ] ;


    return 0;
}

















































Solutions

1)

File: const_ex1s.cpp