skip to content
Ivan's wiki
Site Tools
Search
Tools
Show pagesource
Backlinks
Recent Changes
Media Manager
Sitemap
>
Recent Changes
Media Manager
Sitemap
hpp-concepts
This page is read only. You can view the source, but not change it.
# <concepts> **[<concepts>](https://en.cppreference.com/w/cpp/header/concepts)** provides standard concepts (type constraints) introduced in C++20: `std::integral`, `std::floating_point`, `std::regular`, `std::equality_comparable`, etc. A concept is a compile-time predicate on types that lets you write generic code with enforced requirements. Use concepts to constrain template parameters so that invalid instantiations fail with clear error messages rather than cryptic SFINAE errors deep in the template. This example constrains a template function to accept only integral types, preventing accidental instantiation with floating-point arguments. ```cpp // compile: g++ -std=c++20 -o conceptexample conceptexample.cpp // run: ./conceptexample // description: template function constrained by std::integral concept #include <concepts> #include <iostream> template <std::integral T> T double_value(T x) { return x * 2; } int main() { std::cout << "double_value(5): " << double_value(5) << "\n"; std::cout << "double_value(3u): " << double_value(3u) << "\n"; // double_value(3.14); // error: double does not satisfy std::integral return 0; } ```
Page Tools
Show pagesource
Backlinks
Back to top