C++ Logo

sg20

Advanced search

[SG20] How much is too much with C++20 concepts?

From: Amir Kirsh <kirshamir_at_[hidden]>
Date: Sun, 1 Mar 2020 03:22:17 +0200
A short intro
---
When teaching a feature I'm always trying to bring the minimal example that
is still a good one in terms of best practices. I'm trying to avoid minimal
examples that are convincing but rely on bad practice that might be adopted
by the students.
now to concepts
---
One of the minimal first examples that can be given with concepts is:
void foo(std::integral auto number) { /* */ }
As a substitute for:
void foo(long number) { /* */ }
But then I'm a bit reluctant. Is it really a proper substitute? The two are
not the same. What are the pros and cons of having a template function,
avoiding type conversion, compared to a simple function? Is it a good
example or a misuse? In which cases this is a good substitute?
I tried to raise this question in SO but didn't get much support for it:
https://stackoverflow.com/questions/60470216/how-much-is-too-much-with-c20-concepts
(SO
question has an additional example inside).
This is not a yes/no question - I do believe things might be a *pro* in one
case and a *con* in another. Analyzing the pros and cons for different use
cases can surely assist in understanding the proper choice per each case.
As a teacher I believe this is important, even if not discussed in class,
to understand the pros and cons.
(If there are only pros than the example becomes a new "best practice". And
if there are only cons it becomes an "anti-pattern". I think above is
neither, it probably has good and bad use cases which worth understanding).
Would appreciate your thoughts (on the specific matter, as well as opinions
on understanding pros and cons as a teaching requisite, i.e. is this a
relevant question in your opinion).
Amir

Received on 2020-02-29 19:25:13