C++ Interview Questions

22 Questions
C++ Programming

C++ Programming

Embedded SystemsIoTOther

Question 11

Explain the concept of 'constexpr' in C++ and how it differs from 'const'. Provide examples of its usage.

Answer:

The constexpr keyword in C++ is used to define expressions that can be evaluated at compile time. It was introduced in C++11 and enhanced in subsequent standards. The primary purpose of constexpr is to enable compile-time computation, which can lead to better performance and more predictable code behavior.

Differences Between constexpr and const:

  1. Compile-Time vs. Run-Time:

    • constexpr: Guarantees that the value is computed at compile time.
    • const: Ensures that the value cannot be modified after initialization, but the value can be determined at either compile time or run time.
  2. Functions and Variables:

    • constexpr can be applied to both functions and variables.
    • const is typically used with variables.
  3. Usage Context:

    • constexpr functions can be used in contexts that require compile-time constant expressions, such as template arguments and array sizes.
    • const variables can be used anywhere, but they do not enforce compile-time evaluation.

Examples:

  1. Const Variable:

    const int x = 10;
    int arr[x]; // Valid if x is known at compile time
  2. Constexpr Variable:

    constexpr int y = 20;
    int arr[y]; // Always valid because y is evaluated at compile time
  3. Constexpr Function:

    constexpr int square(int n) {
        return n * n;
    }
    
    int main() {
        constexpr int result = square(5); // Evaluated at compile time
        int arr[result]; // Valid
        return 0;
    }
  4. Const Function:

    int square(int n) {
        return n * n;
    }
    
    int main() {
        const int result = square(5); // May be evaluated at run time
        // int arr[result]; // Potentially invalid if result is not constexpr
        return 0;
    }

Usage:

  • Use constexpr for functions and variables that should be evaluated at compile time. This can optimize performance and ensure that certain computations are done ahead of time.
  • Use const for variables that should not change after initialization but do not necessarily need to be computed at compile time.

Advanced Example:
Combining constexpr with templates to perform compile-time computations.

#include <iostream>

template<int N>
struct Factorial {
    static constexpr int value = N * Factorial<N - 1>::value;
};

template<>
struct Factorial<0> {
    static constexpr int value = 1;
};

int main() {
    constexpr int result = Factorial<5>::value;
    std::cout << "Factorial of 5 is " << result << std::endl; // Outputs: 120
    return 0;
}

In this example, the Factorial struct template uses constexpr to compute the factorial of a number at compile time. The Factorial template recursively computes the factorial value, and the base case is specialized for Factorial<0>.

Summary:
The constexpr keyword in C++ enables compile-time computation, providing performance optimizations and ensuring that certain values are determined during compilation. It differs from const in that it enforces compile-time evaluation, while const only ensures immutability. Understanding and utilizing constexpr effectively can lead to more efficient and predictable C++ programs.

Recent job openings