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
In this repository50 other files it gives its agents.
- analyze-pr-commentsSkill
- architecture-analyzeSkill
- architecture-tradeoffSkill
- askSkill
- code-smellsSkill
- debugging-rocprof-sysSkill
- exploration-explore-codeSkill
- git-commitSkill
- git-gh-clientSkill
- git-prepare-pull-requestSkill
- git-pull-request-statusSkill
- gitSkill
- grill-meSkill
- library-amd-smiSkill
- notify-meSkill
- planning-architectureSkill
- planning-baseSkill
- planning-bugfixSkill
- planning-docsSkill
- planning-featureSkill
- planning-refactorSkill
- programming-cmake-best-practicesSkill
- programming-cpp-design-patternsSkill
- programming-cpp-naming-rulesSkill
- programming-cpp-policy-based-diSkill
- programming-cppSkill
- programming-cpp-stl-algorithmsSkill
- programming-pythonSkill
- pr-review-interactiveSkill
- pr-reviewSkill
- radisha-create-skillSkill
- radisha-helpSkill
- radisha-improve-skillSkill
- radisha-skillsSkill
- radisha-updateSkill
- refactoring-techniquesSkill
- review-architectureSkill
- rocprofsys-buildSkill
- rocprofsys-configureSkill
- rocprofsysSkill
- static-analysisSkill
- testing-gtest-gmockSkill
- testing-pytestSkill
- testing-testplanSkill
- therock-build-to-commitSkill
- therock-commit-in-buildSkill
- therock-first-build-with-commitSkill
- update-pr-descriptionSkill
- verify-pmc-metricsSkill
- watchSkill
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
- Very specific: line 43
Prefer if constexpr over tag dispatch or SFINAE — same result, far more readable.
≈ “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.
Files
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 constexprif 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 computationWhen to Use
| Situation | Use |
| Constant known at compile time | constexpr variable |
| Pure function on compile-time inputs | constexpr function |
| Template branch on type property | if constexpr |
| Precomputed mapping / LUT | constexpr function returning std::array |
| Enforce compile-time evaluation | assign 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 constexprThis is the C++17 equivalent of consteval (C++20 — not available).
Common Mistakes
| Mistake | Fix |
Calling non-constexpr function inside constexpr function | Mark the callee constexpr or extract the computation |
if constexpr outside a template | Only valid in a template context |
Assuming constexpr function always runs at compile time | Assign to a constexpr variable to enforce it |
Using consteval / constinit | C++20 only — use constexpr variable assignment in C++17 |
Mutating state inside constexpr function | Allowed in C++14+ but the function must remain pure (no I/O, no globals) |
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
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.

