r/cpp • u/platisd • Jun 01 '26
Let's get comfortable with concepts (30+ practical examples)
https://platis.solutions/blog/2026/05/02/lets-get-comfortable-with-concepts/In this tutorial you'll learn things like:
- How to replace ugly SFINAE constructs
- Why `requires { false; }` is always satisfied
- The two "flavors" of `requires`, with and without {}
- What concepts and variadic templates look like together and much more!
139
Upvotes
28
u/angelicosphosphoros Jun 01 '26
You website uses too light fonts that are unreadable on white background.
11
u/No_Pitch6452 Jun 01 '26
Good post I enjoyed reading it. One thing I noticed you say “how it looks like” which should be “what it looks like”, I guess you are a native German speaker am I right?
3
u/mikeblas Jun 02 '26
Well, I got lost immediately.
I agree. There's tight yet non-declarative coupling between
AutonomousCarand theCameraobject it has.Really? How so?
Camerais implemented as a concrete class, but we still don't know whatAutonomousCarspecifically requires from it. Or how I might write a replacement, or a test, or safely make any modifications or improvements.Seems like the concrete
Cameraclass was given the benefit of documentation and an interface, too; why are those assumed, but not for the templated implementation?5
u/Mountain-Slice-4037 Jun 01 '26
awesome write up thank you!
5
u/Jovibor_ Jun 01 '26
The first
withshould be changed towithoutin this statement.2
u/xaervagon Jun 01 '26
Good read and very thorough, I'll take anything that lets me use templates effectively without getting bogged down in TMP muck.
After reading this concepts feels like it badly wants to be C#/Java interfaces or metaclasses, but chooses to be deferential to the template design approach.
That said, I agree with the person saying the choice of font color makes this difficult to read.
4
u/UnicycleBloke Jun 02 '26
That was excellent. Educational easy-reading. 😄 I'm not knee-deep into TMP but have dabbled (SFINAE has been a major impediment). I've been aware of concepts but not really had much occasion to use them as static asserts have been sufficient, so this was a good early morning read.
Personally I found it quite useful to think of "requires (T t) { ... }" as a kind of function if only because that makes the syntax more familiar. So long as you remember it isn't executed but is a boolean expression evaluated on whether it compiles.
2
u/Shiekra Jun 08 '26
In using concepts Ive found a rough edge with combining contrained type template parameters and perfect forwarding.
Since the type is deduced with qualifiers, you can sometimes fail a concept because the ref or const version of a type doesnt satisfy the requirements but the underlying type does.
5
u/vI--_--Iv Jun 02 '26
Please don't do that.
Yes, even though a lot of prominent evangelists preached "constrain!" for decades.
Statically checked duck typing is the key selling point of templates.
If your template accepts integer-like types, then it must inclusively accept everything that walks like an integer and quacks like an integer.
And the best way to do that is to leave it unconstrained.
The user might want to define a type that (partially) models an integer for whatever reason.
Rejecting it only because it's not pureblood enough to satisfy
is_integral_vis wrong.Inventing your own
integer_likeconcept for checking that the user type models all the possible integer operations, most of which are not used and never will be used in your template, is also wrong.Concepts are ultimately syntactic sugar for SFINAE, so use them like SFINAE: for overload resolution.
1
u/nikkocpp Jun 02 '26
Great!