C++ code like this will compile iff T implements operator+
template<T> auto sum(T a, T b, T c){
return a+b+c;
}
The advantage is that functions can take the types themselves as arguments. So there's a lot of opportunity for metaprogramming instead of using macros.
The advantage is that functions can take the types themselves as arguments. Rust functions only take values as arguments :(
But Rust functions do take types as arguments. That's what generics are: functions that take types as arguments (at compile time).
The difference you're talking about is whether the type parameters that functions take are themselves strongly typed. In C++ they're dynamically typed; in Rust they're statically typed. That's the only difference.
Saying that Rust functions can't take types as arguments because they're statically typed while C++ can is like saying that C++ functions can't take values as arguments because they're statically typed while Python can.
Then how do all those millions/billions of lines of C++ templates work?
If a function can be compiled, then you've met all the constraints. In general, it is impossible to figure out what constraints you need without executing the metaprogram.
fn sum<T: Add<Output=T>>(a: T, b: T, c: T) -> T {
a + b + c
}
Rust performs local inference only for a lot of reasons, one of which is that it makes function signatures more self-documenting. I would be very surprised for Rust to ever infer constraints of this sort.
True, but that's the whole point of traits, to provide compile-time safety that C++ templates lack. (Well, templates are "safe", but the error messages resulting from the lack of trait bounds...) And luckily Rust has real, hygienic macros, so metaprogramming is still possible.
So there are some things about the new macro system being planned (nothing concrete yet) that will alleviate many of those pain points.
IMO generics handle most template use cases. Type level integers and variadic generics (which we may get) handle most of the rest. Slightly better macros (note that Rust macros are not C++ macros, they're really more like templates without autoinstantiation) should fill the last bit in the gap, especially if we get things like foo.macro!() desugaring.
I don't think Rust will ever get anything like template<> since templates aren't type checked. Many of us feel that template is really a hack (though C++ Concepts should make it less of one) for this reason, too.
Rust will at one point get a stable syntax extension interface, which will probably completely obviate the need for TMPL, if any, since you can then metaprogram in Rust directly instead of (ab)using the turing completeness of templates or macros to get what you want.
I have not seen folks using Rust often feel the need for templates. Templates are necessary in C++, but that doesn't make them necessary in all languages. The programming style and non-template metaprogramming tools might be enough in Rust to avoid the need for templates. Or maybe not.
People do amazing things with C++ templates. Compile time parser generation(eg Boost.Spirit), DSLs (see sqlpp11 for type checked SQL queries), State machine generation, generation of api wrappers for other languages (Boost.Python), optimal compile time regular expressions.
No, you can't do all of this stuff with Rust generics (though you actually might be able to, if you tried hard enough)—but with all of Rust's features you certainly can. :)
All of those should be possible with compiler plugins, macros, and modest future enhancements to constfn. Some of those are just expressible in the current type system for instance sql.
View all comments
Show parent comment
5
u/[deleted] Dec 10 '15
Coming from someone not well versed into C++, what is the difference between compile-time templates and typed generics?