isocpp / isocpp/CppCoreGuidelines

T.126 Use a common function for runtime/compile-time evaluation, and `std::is_constant_evaluated()` if necessary

Open
#2,095 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
CSS
Stars
45.3k
Forks
5.6k
PR merge metrics
No merged PRs in 30d

Description

Here is a suggested guideline for std::is_constant_evaluated(), which is currently not mentioned anywhere.

The main motivation is to avoid code duplication, and to unify the interface of constexpr/non-constexpr functions.

This suggestion could also be phrased in terms of if consteval, but C++23 is still not widespread.

T.126 Use a common function for runtime/compile-time evaluation, and std::is_constant_evaluated() if necessary
Reason

Avoid code duplication, avoid refactoring when implementing more functionality in constexpr functions.

Example, good
constexpr double pow(double base, int exp)
{
    if (std::is_constant_evaluated()) {
        double result = 1;
        for (int i = 0; i < exp; ++i) {
            result *= base;
        }
        return result;
    }
    else return std::pow(base, exp);
}

// good, runtime and compile-time evaluation use the same function
constexpr double pi_cubed = pow(std::numbers::pi, 3);
double pi_cubed = pow(std::numbers::pi, 3);
Example, bad
constexpr double constexpr_pow(double base, int exp)
{
    double result = 1;
    for (int i = 0; i < exp; ++i) {
        result *= base;
    }
     return result;
}

// bad, runtime and compile-time use different functions that do the same
// if std::pow is made constexpr, we may have to replace all uses of constexpr_pow
constexpr double e_squared = constexpr_pow(std::numbers::e, 2);
double e_squared = std::pow(std::numbers::e, 2);
Note

In C++23, write if consteval instead of if (std::is_constant_evaluated()).

Warning

Do not write if constexpr (std::is_constant_evaluated()). This is a common mistake and the condition is always true.

Enforcement

Flag separate runtime/compile-time versions of the same function. Merge runtime function into compile-time function.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No file or test is named in the issue. Start by locating the existing guideline entries and reviewing nearby rule wording; done means incorporating T.126 with its rationale, examples, note, warning, and enforcement guidance.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
documentation
Issue type
Documentation
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.