Would Generics break code?

I'm not sure if this question is better suited for r/cpp_questions or not but here is it. If the type checking for generic functions was to be done before instantiations like many other languages do, how much code would break. One obvious answer is all of it because we didn't have concepts till c++20 but just assume that it is somehow taken care of. I'm more interesting in knowing about what kind of code would we not be able to write anymore.

4 comments · 44% upvoted · other discussions

comments

gnolex

C++ templates are not like generics in other languages. Trying to change templates to generics would completely break the language.

GrammelHupfNockler

Concepts are like duck typing, while true generics require explicitly defining interfaces by what they provide, not how they can be used. It should be possible to turn most if not all concepts into matching interfaces, but it would be a significant undertaking. An earlier design of C++ concepts would actually have been early-checked generics. The closest thing we have to it is the `generic` implementation in the Circle compiler, which shows at least that it is feasible to add generics to C++.

disciplite

Carbon and Circle generics were both suggested as additive features, rather than replacing templates. Both languages have a concept of templates and generics, because so far no one has figured out a way to make a redesign like that possible. Imho, it's probably impossible and also not even desirable to fully replace templates with a feature like that.

Possibility_Antique

There are only two possibilities that I see: 1. RTTI must be used to allow type erasure 2. The set of types acceptable by a template are closed, and no new types can be added to the set of acceptable types (thereby defeating the point of having genetics).

In my opinion, C++ generics are pretty perfect. You'd break most of the standard library if this behavior changed, and I would view it as a huge step backward.