3 ms·
C++ 20 Concepts are still just duck typing, although the syntax is less awful than with SFINAE. C++ 0x Concepts were something more, but those were never actual
by tialaramex 4y ago
C++ 20 Concepts are still just duck typing, although the syntax is less awful than with SFINAE. C++ 0x Concepts were something more, but those were never actually standardised, and as Sean Baxter found it's difficult to piece together what exactly was intended in the never-finished standard.
The big give away is, can I use this to care about semantics or just syntax ?
More over though, although C++ 20 Concepts are checked early (which can result in better diagnostics) they are not constraints on how the type is actually used.
Suppose we have a library function with a parameter v, whose type is constrained to be a Bird. The Bird concept says it has a flap_wings() method, and a predicate named swim() which checks whether this bird can swim. Our function does use v.flap_wings() but it also calls v.chirp() a function which is not defined for the Bird concept.
The compiler is fine with this, as it stands, our library is good. Users shall call this function with a parameter v which is a Bird. Secretly the compiler knows that as well as being a bird, v.chirp() needs to work, but that's OK, no doubt all users will arrange this. Right? Perhaps it will be documented somewhere.
When a user of our library tries to use an Ostrich, which is indeed a Bird because it has flap_wings() and the swim() predicate, the compiler doesn't like that. Somewhere in the dozens of lines of diagnostics is the fact that Ostrich doesn't have a chirp() method and so it's not suitable despite being a Bird. This is probably not what we, as library authors, intended. Too bad.
- pjmlp 4y agoYou can fix that with some static analysis/IDE tooling help, giving an error when the implementation makes use of stuff not declared on the concept requirements. However I do agree it is a kludge.
- IshKebab 4y agoInteresting. I haven't actually used concepts yet. Kind of sounds like type hints for generics, in the same way that type hints for e.g. Python are very best-effort rather than actually sound.