agentleFS
ROCmROCm/rocprofiler-systems-skills/skills/programming-cpp-constexpr/SKILL.md

programming-cpp-constexpr

Skill5 starsChanged 16 days agoMITView on GitHub

Use when moving computation from runtime to compile time — precomputed lookup tables, compile-time constants, type-based template dispatch, or zero-cost conditional branching with if constexpr; use when a function or value could be evaluated before the binary runs

The file

We split this skill into the rules it gives an agent

1 rule. About 0 are common, 0 are distinctive, and 1 is very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈0

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Prefer if constexpr over tag dispatch or SFINAE — same result, far more readable.

    line 43

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

C++ constexpr — Compile-Time Computation (C++17)

Move work to compile time: zero runtime cost, no dependencies, trivially testable.

Core Forms

constexpr variable

constexpr int cache_line_bytes = 64;
constexpr double pi = 3.14159265358979;

constexpr function

Evaluated at compile time when all arguments are compile-time constants; falls back to runtime otherwise:

constexpr int square(int x) { return x * x; }

constexpr int a = square(8);  // compile time
int b = square(n);            // runtime — n is not constexpr

if constexpr (C++17)

Compile-time branch — the non-taken branch is not compiled, not just skipped:

template <typename T>
auto process(T val) {
    if constexpr (std::is_integral_v<T>)            return val * 2;
    else if constexpr (std::is_floating_point_v<T>) return val * 2.0;
    else                                            static_assert(false, "unsupported type");
}

Prefer if constexpr over tag dispatch or SFINAE — same result, far more readable.

Compile-time lookup table

constexpr auto build_lut() {
    std::array<int, 256> t{};
    for (int i = 0; i < 256; ++i) t[i] = i * i;
    return t;
}
constexpr auto lut = build_lut();  // built entirely at compile time

int val = lut[x];  // pure array access at runtime — zero computation

When to Use

SituationUse
Constant known at compile timeconstexpr variable
Pure function on compile-time inputsconstexpr function
Template branch on type propertyif constexpr
Precomputed mapping / LUTconstexpr function returning std::array
Enforce compile-time evaluationassign result to constexpr variable

Testing

constexpr functions are pure — no side effects, no dependencies — so no mocking is ever needed.

// Compile-time: catches bugs before the binary exists
static_assert(square(8) == 64);
static_assert(lut[4] == 16);

// Runtime: works with GTest unchanged
TEST(Square, PositiveInput) { EXPECT_EQ(square(8), 64); }

Forcing Compile-Time Evaluation

Assigning to a constexpr variable forces compile-time evaluation — the compiler errors if it cannot:

constexpr int result = expensive_fn(args);  // fails to compile if args are not constexpr

This is the C++17 equivalent of consteval (C++20 — not available).

Common Mistakes

MistakeFix
Calling non-constexpr function inside constexpr functionMark the callee constexpr or extract the computation
if constexpr outside a templateOnly valid in a template context
Assuming constexpr function always runs at compile timeAssign to a constexpr variable to enforce it
Using consteval / constinitC++20 only — use constexpr variable assignment in C++17
Mutating state inside constexpr functionAllowed in C++14+ but the function must remain pure (no I/O, no globals)

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.