by pjmlp · 10y ago · 0 comments
However if it wasn't for my goal to write portable native code across mobile OSes or the occasional weekend dive into LLVM, I would hardly touch C++ nowadays.
Waiting until C++20 for them, will mean 4 more years for alternatives to slowly erode C++'s usage where its features aren't really that critical for fulfilling the customer's use case.
On the other hand, people are still writing new Fortran code everyday, so maybe those 4 years won't make a difference to those highly invested into C++.
For example, last time I searched for it, most embedded and real time OS compilers were mostly C++11 compliant.
How so? Their use cases seem orthogonal.
From what I understand it is the preprocessor macros that make this really awkward. Personally I would like to see C++ abandon those in favor of a "real" macro system.
If there was some kind of parametric polymorphism in Fortran I would die happy.
Also modules are already on VS 2015 Update 1, just not as final design.
I don't like templates at all, I think it's just an even greater hack than how the preprocessor might be used in C. The only place I have liked macros [essentially] is in Lisp.
Something that bothers me about C++ is when a program might instantiate a template like this: std::vector<int>
I'm sure vectors of ints is quite common. I wish C++ could share that generated object code (not the instantiated template at compile-time). I've heard of `extern template` but I don't think it gets used. Even then I wish you didn't have to tell the compiler to look for external versions of the template instance. I wish the compiler could aggressively look for and link to instances that have the same binary representation - even if they have disparate type information. That kind of analysis would probably be easier if C++ had a defined ABI.
I am not a C++ person, so I am sure I'm wrong more than a few times in this post. I wish C++ had a standard class to represent classes, so then we could construct a very generic object heirarchy like Ruby. I don't just mean support for reflection. I also want partially-defined classes for monkeypatching, but people tend to disagree with that for ethical and performance reasons.
I've been happy to see a lot of languages enable expressiveness/freedom to do whatever the hell you want in the last 10 years - instead of people forcing their mentality of how you should code and think about problems.
</ramble-ramble>
I am happy concepts were not added. The preprocessor &and& templates should die.
Today your wish comes true, because this is something that compilers do and have done for a very long time. Redundant definitions of templated functions which didn't get inlined always get dropped at link time. VC++ and the Gold linker also have a feature called "Identical COMDAT Folding" which will agreesively merge functions with different names but identical bodies, which can merge distinct template instantiations in the case where the concrete type doesn't effect the compiled representation.
extern templates are orthogonal to this whole thing; they're purely a compile speed optimization and have no effect on the end binary after linking.
I wonder how many things aren't aggressively inlined in C++..
---
Are you saying `extern template` is used like a `#pragma once` just for the template? I thought it had a greater effect on linking. :(
My concern is that even if the generated object code for std::vector<int> and a binary-compatible type like std::vector<something> are deduplicated in your codebase, that same object code for that specialized type of vector probably exist in several different programs throughout my system. (as an example)
Has it should be, actually. Freezing inlining algorithms in a language specification hinders future optimization algorithms.
Chandler Carruth has done quite a few interesting talks how the clang inliner works.
The generated object code for thing<int> and thing<double> are likely to not be identical, except for where the types are manipulated. Because thing<int> and thing<double> generate entirely different classes, they can actually be radically different. First, C++'s resolution rules allow them to be actually different C++ code. But even if it reuses the C++ code, because that code then goes through the optimization phases, what comes out the other side may not have the kind of trivial relationship you're thinking of.
I don't think this is unique to C++; it should be true of any language that has non-type erased generics that generates native code. I'm thinking of Haskell, OCaml and Rust - but if people who know those languages and compiler chain better think I'm wrong, please speak up.
Personally, I find C++'s templates to be extremely useful and powerful. I also think they are demonstrably not as nearly as hackish as the C preprocessor, as evidenced by the fact that they actually have knowledge of types. The C preprocessor is just textual substitution, while C++'s templates are far, far from that.
On the other hand, Rust generics are monomorphized at compile time, which is closer to how C++ templates work. (The most important difference is that Rust generics are type-checked at definition time, rather than instantiation time.) Another language implementation that uses compile-time monomorphization is MLton, a whole-program Standard ML compiler.
Ideally, the language should offer the programmer two kinds of polymorphism: (0) second-class, which is less expressive but easier to type-check and more amenable to optimization, (1) first-class, with the opposite tradeoff.
This of course means that compile times are a bit longer, as a given template may be instantiated by the compiler many times, but in terms of run-time and final object size it should be optimal.
I think this behaviour also makes more sense when you think about inlining -- a lot of template code is actually doing something quite simple under the hood, so by having the compiler instantiate your template each time, you potentially allow it to inline the code qithout relying on a LTO, which may not have been feasible when this whole mess was designed.
"Defined ABI" is something of a problem, as C++ is required to work on a variety of current and future hardware platforms, with disparate calling conventions and the like.
Monkeypatching is not something I understand how or why one would attempt in C++, but there's always the option of building yourself a C-style collection of function pointers and swapping them out at runtime. Or, may God have mercy on your soul, Windows COM.
Templates are C++'s metaprogramming feature. Without them there would have to be another means of implementing generics. The syntax is often ugly and occasionally bananas, but they're one of the things that make C++ what it is.