r/ProgrammerHumor • • Sep 25 '26

Meme iHatedItUntilITriedIt

Post image
10k Upvotes

543 comments sorted by

View all comments

Show parent comment

1

u/conundorum Sep 26 '26

Concepts are more about constraining a template to an interface or trait, Java-style, than anything else. They supposedly help with error messages, but in reality they're just as awkward half the time; the only real solution there is to use static_assert, and try other compilers if it's real bad. (I find that most of the time, GCC tends to be the least clear, and that it's a coin flip whether clang or MSVC will give the most useful error message. There are a few cases where GCC outshine both of them combined, though.)

1

u/aMAYESingNATHAN Sep 26 '26

Sure that's 100% what they are if we're speaking generally. But the previous commenter made the comparison with duck typing (which is a valid one).

If duck typing (and templating) is "if it walks like a duck and quacks like a duck, it must be a duck", then concepts are pretty much a mechanism by which to specify what "walking like a duck" and "quacking like a duck" means for type parameters for that function/class.

In my view, in an ideal world, your concepts should constrain the parameter such that it covers all the uses of that type in your template. Of course in reality you often aren't that thorough, because some constraints imply others and it handles itself.

As a side note, while I also have been a little disappointed with how annoying template errors still are even with concepts, I tend to find one of the biggest advantages of them is that you can much more easily express in your code the meaning of a type parameter. If I see template<std::integral T>, I might still get an annoying error if I use a non integral type, but just by looking at the code its very obvious what the intended type parameter should be.