CKTIS / C++
concept — a named constraint on template arguments
The concept keyword defines a named constraint on template arguments. It lets you reuse requirements checked during compilation and restrict overload availability without adding runtime checks.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
template<typename T>
concept ConceptName = constraint_expression;
template<ConceptName T>
void f(T value);
// Alternative constraint syntax:
template<typename T>
requires ConceptName<T>
void g(T value);concept defines a concept: a template that specifies constraints on its arguments. The basic form is template<typename T> concept ConceptName = constraint_expression;. Place the definition at namespace scope, not inside a function or class. A concept is neither a class nor an interface requiring inheritance.
ConceptName<T> expresses whether the constraint is satisfied. The spelling template<ConceptName T> constrains a type parameter; an alternative is template<typename T> requires ConceptName<T>. During overload resolution, function constraints must be satisfied. An unsatisfied constraint excludes that candidate; if no matching function remains, the call produces a compilation error.
Do not confuse concept with requires: the former names a constraint, a requires clause constrains a declaration, and a requires { ... } expression checks requirements and produces a bool result. In the example, typename T::value_type; checks that a valid type name exists. It does not construct an object or check program behavior at runtime.
HasValueType<Item> is true and HasValueType<int> is false. The call inspect(Item{}) returns 7, and main prints that value through std::cout. Concepts cannot be explicitly or partially specialized. The source is an evolving standard draft, not a frozen C++23 text; the example targets compilation with -std=c++23.
Examples
#include <iostream>
template<typename T>
concept HasValueType = requires { typename T::value_type; };
struct Item {
using value_type = int;
};
template<HasValueType T>
constexpr int inspect(const T&) {
return 7;
}
static_assert(HasValueType<Item>);
static_assert(!HasValueType<int>);
int main() {
std::cout << inspect(Item{}) << '\n';
}
Result
7
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.