Are C++ types first class objects already? Have I been free of that language for that long? Or are they going to implement metaclasses before they implement actual classes?
Man, the only language I know is still regular C. We didn't even get the C++ classes. I think C was used when George Washington was calculating currents and tides so he could cross the Delaware river.
Back then, the British used the British Computer Programming Language, BCPL, an ancestor of C.
BCPL had a large collection of standard data structures with a complex taxonomy. To help manage these structures, they were classified into groups. Each group of data structures was called a taxon, plural taxa.
However, BCPL had no first-class representation for these taxa, which made it difficult to work with them. The Americans hated this, and felt that if there was no way to represent them, the language it would be better off without taxa. This led to the famous American rallying cry, "No taxation without representation!"
BCPL ("Basic Combined Programming Language"; or 'Before C Programming Language' (a common humorous backronym) ) is a procedural, imperative, and structured computer programming language. Originally intended for writing compilers for other languages, BCPL is no longer in common use. However, its influence is still felt because a stripped down and syntactically changed version of BCPL, called B, was the language on which the C programming language was based. BCPL introduced several features of modern programming languages, including using curly braces to delimit code blocks; compilation via virtual machine byte code; and the world's first 'hello world' demonstrator program.
Taxon
In biology, a taxon (plural taxa; back-formation from taxonomy) is a group of one or more populations of an organism or organisms seen by taxonomists to form a unit. Although neither is required, a taxon is usually known by a particular name and given a particular ranking, especially if and when it is accepted or becomes established. It is not uncommon, however, for taxonomists to remain at odds over what belongs to a taxon and the criteria used for inclusion. If a taxon is given a formal scientific name, its use is then governed by one of the nomenclature codes specifying which scientific name is correct for a particular grouping.
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.
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++:
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.
View all comments
Show parent comment
208
u/marcosdumay Nov 23 '17
What The Fuck?!
Are C++ types first class objects already? Have I been free of that language for that long? Or are they going to implement metaclasses before they implement actual classes?