Using Concepts in Templates and auto
Constrain function templates and abbreviated function templates with concepts.
Using Concepts in Templates and auto is a free C++ Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.
Three Syntaxes Recap
You can apply a concept three ways: as a template parameter prefix, as a requires clause, or with abbreviated function template syntax.
Prefix Syntax
Replace typename with the concept name.
template <std::integral T>
T add(T a, T b) { return a + b; }requires Clause Syntax
Add requires after the template parameter list. Most flexible — supports complex conditions.
template <typename T>
requires std::integral<T> || std::floating_point<T>
T add(T a, T b) { return a + b; }Trailing requires Clause
Place requires after the function signature — useful in member functions where the constraint depends on parameters.
template <typename T>
class Container {
public:
void sort() requires std::sortable<T> { /* ... */ }
};Abbreviated Function Templates
Use auto in parameters and constrain it. The compiler synthesizes the template under the hood.
void greet(std::convertible_to<std::string> auto name) {
std::cout << "Hello " << name;
}Multiple Auto Parameters
Each auto can have its own concept. Each auto creates an independent template parameter.
auto multiply(std::integral auto a, std::integral auto b) {
return a * b;
}Concepts with Class Templates
Same three syntaxes apply.
template <std::movable T>
class Buffer { /* ... */ };
template <typename T>
requires std::default_initializable<T>
class Storage { /* ... */ };Constrained Lambdas (C++20)
Lambdas can have explicit template parameters with concepts.
auto print = []<std::integral T>(T x) {
std::cout << x << " is integral";
};
print(42);Concept-Based Overload Resolution
The compiler picks the most constrained overload that matches. More specific concepts beat less specific ones.
void process(std::integral auto x) { std::cout << "int\n"; }
void process(std::signed_integral auto x) { std::cout << "signed\n"; }
process(42); // calls signed_integral versionConstrained Member Functions
Members of a class template can have their own constraints.
template <typename T>
class Optional {
T value_;
public:
void clear() requires std::is_trivially_destructible_v<T> { /* fast */ }
void clear() { /* general */ }
};Default-Initialized Constrained Types
Combine requires with default values to write clean, intention-revealing templates.
Style Guide
Use the prefix syntax for the common case. Switch to requires when constraints are complex or involve multiple types. Use abbreviated function templates for short, focused functions.
Quick Check
What is the abbreviated function template syntax for "a function that takes any integral value"?
Recap
Apply concepts as prefixes, in requires clauses, or in abbreviated function templates. The most constrained matching overload wins. Use the cleanest syntax that fits — prefix for simple, requires for complex, abbreviated for short.
Learn C++ with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “Using Concepts in Templates and auto” lesson free?
Yes — any 3 lessons of the C++ Academy learning path, including “Using Concepts in Templates and auto”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.
What will I learn in “Using Concepts in Templates and auto”?
Constrain function templates and abbreviated function templates with concepts. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C++ Academy?
No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.
How long does the “Using Concepts in Templates and auto” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C++ Academy lesson?
Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Why Concepts vs SFINAE
- Defining Concepts with requires
- Using Concepts in Templates and auto
- Common Standard Concepts Integral Ranges