Daily C++ Tip 2

Constrain a function with a concept, in the parameter list

A concept in the parameter list rejects wrong argument types at compile time.

An abbreviated function template puts the constraint where the parameter is declared. A wrong argument type is a compile error naming the concept, not a page of template errors.

#include <concepts>
#include <iostream>

// Abbreviated function template: the parameter's type must satisfy integral.
auto doubled(std::integral auto v) { return v * 2; }

int main() {
    std::cout << doubled(21) << '\n';
    // doubled(2.5); // compile error: double does not satisfy integral
}

Observed output:

42

Facts verified 2026-11-04. The code on this page was compiled and run on the box with GCC 16.2.0 at -std=c++23 -Wall -Wextra; the outputs quoted are the observed outputs. MSVC was not tested.

Back to the daily tips index

Keep Exploring

Keep exploring the docs, samples, and free tools to continue your evaluation.