top | item 24962028

(no title)

fluffy87 | 5 years ago

C++ templates are „untyped“ or very „weakly“ typed at the kindest, while Rust generics are „strongly“ typed.

That’s the main difference in these languages features.

Saying that Rust generics are „weaker“ than C++ templates is technically incorrect, since they are actuall „stronger“.

Using these terms to mean something else is definitely „weird“.

discuss

order

yokaze|5 years ago

> C++ templates are „untyped“ or very „weakly“ typed at the kindest, while Rust generics are „strongly“ typed.

Can you provide your definition of strongly typed?

According to my understanding, C++ templates are evaluated at compile time, and the resulting instance has to qualify all the static type checks of standard C++. The deduced types is always checked, and if there is no substitution fullfill the type check is found, it raises a compile error.

The errors are super ugly, as there are no type constraints for template parameters, but funnily enough the template errors in C++ reminded me of playing around with Haskell without using function declarations. I would find it odd to call Haskell though untyped or weakly typed because of that.

lifthrasiir|5 years 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.

gpderetta|5 years ago

then again, the strongly and weakly typed monikers have no defined meaning and people will argue endlessly about definitions to the point that have very little value.

edit: the static/dynamic distinction pointed out elsethread is a much better description.

kupopuffs|5 years ago

In terms of ease of expression and using mental power, C++ templates may be a more powerful tool for some.

Like you're describing the type system, they may be talking about actual effectiveness.

Like productivity diff between python and others

Basically, I'm not smart enuf to use rust

TheDong|5 years ago

If they meant "A more powerful tool", the right way to express that is "more powerful", not "weaker/stronger".

However, if you look at that part of the article, they justify that statement by linking to this post [0] which is explicitly about how rust actually type-checks generics more strongly. The linked post has the opposite implication of what the author took it to mean, and that link makes it clear that they were referring explicitly to how they're typed, not to "actual effectiveness".

[0]: https://users.rust-lang.org/t/generic-functions-c-vs-rust/21...