3 ms·
Before C++20 templates were untyped. Since C++20 it's possible to define structural types for templates via concepts. Concepts are an explicitly structural type
by Kranar 5y ago
Before C++20 templates were untyped. Since C++20 it's possible to define structural types for templates via concepts. Concepts are an explicitly structural type system for templates.
Nominal typing of generic code would be what most other languages that supports generic programming use. For example Rust has traits where one defines a named trait and must explicitly specify that a type satisfies a trait by name.
In C++ there is no such means, a concept does strictly structural checks (via constraints) to determine if a template satisfies a concept, no names are involved. In other words, a type T satisfies a concept C defined by a constraint R if and only if a T can be substituted for C's placeholder type during the evaluation of R.
- gpderetta 5y agoEverything you said is very much true, but there is a kind of nominal typing even in template land via (partial) specialization of special trait classes. For an example see std::tuple_size/tuple_element and their relation with (ironically) structured binding.
- ncmncm 5y agoRight: C++ offers a continuum between pure structural typing and nominal typing. Author chooses according to need. Failing to support structural typing is not an advantage.
- gpderetta 5y agoYes, as usual, name a language feature and it is probably somewhere in C++ :) (mind, I'm not complaining).