Hacker News new | ask | show | jobs
by lifthrasiir 2056 days ago
It should be said that C++ templates are dynamically typed (not weakly) and Rust generics are statically typed. But otherwise it is correct that Rust generics give a stronger guarantee than C++ templates.

It is beneficial to view C++ as a two-stage programming language, where the first stage runs templates and emits instances and the second stage runs them in the actual target. When C++ templates are said to be "untyped", that's really about the first stage [1]. You can't be very sure that templates act as intended for all valid inputs because it can't statically determine types before the first stage. Of course you can still write almost-working templates.

[1] Specifically the template invocation. C++ templates do have types for compile-time expressions and for that reason it was a wise move to move most of them into constexpr functions.