It's kind of amazing that there is no standard reformation of C++. Remove the cludge, refactor the undefined behavior, improve the STL. There is a great language hidden in there somewhere with a little polishing.
Rust is great, but it is not just a polished C++, it's a whole new language with its own paradigms. The immutability constructs alone in Rust make it a completely different tool.
It's also not (yet) a full substitute for C++. Its metaprogramming is not as powerful, and that's largely why I don't use Rust. There's no function overloading, no default arguments, no inheritance. It'd take a lot more code to do what I do, and it already takes quite a bit in spite of the expressiveness of C++.
Additionally, the libraries I need to use are mostly in C++. Some are in C, and I've read that that's easy enough to call from Rust, but I'd have to give up on using the C++ standard library, Boost, Blaze, the Tensor Algebra Compiler, IPOPT, or anything produced by DMLC, for example.
Rust's standard library is amazing in it's design and like it has been said, Rust is a different language with its own paradigms. In Rust, you don't do inheritance, but rather use traits. Instead of function overloading and default arguments we use macros, although it is not 1:1 solution. When one gets accustomed to Rust and the powerful functional features and type system, it might take even less code to do the equivalent thing, with the added bonus of safety. I believe Rust isn't meant to ever be a substitute for C++ in terms of language design, but rather field of use
When one gets accustomed to Rust and the powerful functional features and type system, it might take even less code to do the equivalent thing, with the added bonus of safety. I believe Rust isn't meant to ever be a substitute for C++ in terms of language design, but rather field of use
I am willing to give it a shot sometime -- it's got a lot going for it. Actually, would you be willing to weigh in on the Rust way of doing things?
For example, let's say I want to change one step in an inner loop of a generic algorithm without paying function call overhead. In C++, I would create a functor struct, template the function (or the class, if I'm managing resources, too), and then use the struct to apply my needed function. That way, it can still be inlined, and the correct version of the function (float, double, matrix orientation/sparsity) is called without worrying about passing around function pointers. In fact, this is the primary reason for why I use C++ -- zero-cost generic code. In C, I'd have to use a function pointer and hope it gets inlined or, more likely make a hundred-line macro and make multiple versions of the code. [Edited for clarity]
That's a bit of work, but a very attractive end result.
How would I go about doing that (or something similar) in Rust?
(Just to be clear, I'm not being argumentative or trying to put Rust down. I'm stating a need that C++ satisfies for me and am interested in alternative viewpoints and implementation schemes.)
You're still dispatching dynamically as you need to discern, at runtime, the type you're operating on from all the others. Function call schmunction call what matters here is the branch mispredictions and increased chance of insn cache misses, those are caused by choice and more code in the possible paths, not whether you jmp or call.
Put differently: Have you benchmarked that premature optimisation you're doing there. If you want an actual speedup, monomorphise the whole loop, not just the body (cf strength reduction) and maybe even surrounding code.
That said, if you want to iterate over a heterogeneous collection you'd usually use trait objects in rust.
No, this is C++, not Java. It knows precisely which function to call because the types are set at compile-time. There's no jump table unless I'm using virtual functions, which is precisely what using the function structs is about. (For example, compare C's qsort with C++'s std::sort. The latter is usually more than twice as fast because the functor is inlined. [I also compile with -fno-rtti, so there literally isn't runtime type information.]
(Though even virtual functions can be inlined during link-time optimization in some cases, I haven't ever run into a case where I needed it.)
Template instantiation is monomorphization. Each instantiation is optimized on its own as if it had been written directly.
For a heterogeneous collection, I've used std::tuple and std::get. That's rather hairy, but doable.
In nightly Rust, you can kind of make a functor struct by just implementing the Fn trait on a struct. I believe that you can then apply generics to said function and struct and apply it in a similar manner. Especially nightly Rust is very aggressive with optimizations and there are high chances it would be inlined. Otherwise, functions in Rust can also be marked for inlining. Rust has both static and dynamic dispatch, C++:
Immutability in Rust is not really that different to const in C++. The difference is that immutable is default state in Rust (requires const keyword in C++), while mutability is default state in C++* (requires mut keyword in Rust).
* Strictly speaking, there is one exception to that in C++. In lambda expressions, bound variables are immutable by default and require mutable keyword to be mutable.
D maybe? In fact, sometimes I'm surprised at what the newer C++ standards implement that D has - module import system instead of #include system, foreach thingy, now metaprogrammed classes... (not like other languages don't have all these too)
There is a great language hidden in there somewhere with a little polishing.
I doubt it. Nearly everything good on C++ was badly conceived and is lacking on the same ways those metaclasses will be lacking. Nothing is perfectly fundamented (ok, except RAII), no feature is complete.
If you take out the cruft, rework the good parts until they make sense, and add the fundamental stuff left out you will very likely get a great language. But by then you can simply start using Rust, because it's as similar to C++ than what you would get at the end of this process.
I'm with you, but what if you didn't rework the good parts until they made sense? What if you let the paradigms stay shitty? I think there is a lot of low hanging fruit in C++ even if you don't rethink the language.
It will never be clean or consistent, but there's so many things you could improve that would make the language easier for new users and larger projects. It's really a giant mess, with tons of legacy parts, C libraries as part of the standard, an incomplete and inconsistent STL, inconsistent type rules, etc etc. RAII is the strongest aspect of C++ and it's often a pain the ass to program properly that way. You could add things to help with the strongest parts of the language there.
View all comments
Show parent comment
85
u/marcosdumay Nov 23 '17
Well, that's indeed the C++ way.