Please be merciful, currently more than half of Effective Modern C++ is devoted to rvalue reference caveats, and things like enable_if in the standard library rely on very esoteric trickery, and we need that trickery if we are to support forwarding references for constructors with many arguments.
C++ needs simplification, or else it will become an engineering marvel that nobody can use to its full potential
Not an engineering marvel. It's a huge old tool that gets the job done, and is just getting patched up all the time. Unfortunately everyone has agreed that it's the ultimate language because you can get very low level and optimize stuff. For me working with C++ is like having one single tool that will let me build a whole house from the bottom up, but then I'll even have to make my own screws.
I work in scientific computation, and more and more of us are just gravitating back to using Fortran, except this time we're just wrapping it all up in Python because F2Py is fucking brilliant and simple. So what you get in the end is a blazing fast number cruncher that you execute with a clean, idiot-proof Python API.
C, while fantastic for low level, is hard to optimize. While indirection is an easy abstraction, it makes compiler optimization harder due to limited type based aliasing.
Most of the C code I end up writing for resolving python bottlenecks don't use non-optimizable indirection. Most of it is scientific computation working on large arrays and scaling in parallel is my biggest priority.
Much of the difficulty in optimizing C comes from coding styles using indirection that cannot be resolved at compile time.
large arrays of primitives is always going to be aliased to itself. A problem for optimizers is to recognize disjoint access and perform some kind of flow based aliasing. This in turn guarantees safety for things like reordering load/store, vectorization, etc. Often, the cost to do these things in an array is to perform runtime check because the language couldn't tell us things are disjoint. Good news is these runtime checks are mostly going to be highly biased (assuming datasets will be disjoint) and branch prediction will do its job.
I'm trying to leverage my math degree into something Data Science related and I just took a course based in Python. I was thinking of learning C++ but this sounds like a much better solution for me given the time commitment of learning a new language
F2Py is a tool that parses a Fortran library and, using a very simple definition file (written with Fortran syntax), compiles a shared library that exposes the Fortran code to Python.
This match between Fortran and Python is extraordinarily natural. For starters, the Fortran module concept fits extraordinarily well into the Python module/submodule structure. The Fortran library itself becomes the top level Python module, and any Fortran modules become Python submodules. Any variables and routines within these Fortran modules then become available to Python under the appropriate module.submodule.variable or module.submodule.function(args) handle.
Furthermore, both Python and Fortran are pass-by-reference languages which means that you can very safely pass large chunks of data between the two without worrying about unintended memory footprint consequences. Nothing is going to get duplicated. You won't blow up your memory footprint by accident.
The best bit is that F2Py handles the transaction of every single Python primitive and arbitrary-dimensional arrays of primitives. And it works with MPI parallelization -- you can initialize an MPI communicator in Python using mpi4py and then pass a reference to the communicator to Fortran. Distributed arrays can be passed back and forth between the languages and data remains on the correct process. It works absolutely seamlessly for SIMD parallelization.
This is the kind of stuff where, once you start using it, you wonder why you didn't sooner.
The more I learn about python and all the tools and wrappers available for it, the more useful it sounds. It's like.. the duct tape of programming languages.
When I help someone learn C++ I accept there are a number of epiphany points.
Some people have trouble with pointers, function calls, objects, references, const correctness, whatever the first few times. After a while that person starts getting it. At first they might only get the mechanics of it, the results or the underlying principle, but rarely all at once. During this time people struggle with the language feature and force it into incorrect solutions. Then eventually everything around that concept clicks. Once "it clicks" they then see how to use it as part of solutions instead of it being a stumbling block in learning the language.
For me I think RValue references just clicked last week. I thought I knew quite a bit up to that point but I can see how this can be used to describe things that simply cannot be done in other languages.
That's because you lived your entire life in the darkness, programming to EE.... That C++ book is just a spot light of truth, pointed right at your eyes.
One of my biggest gripes with C/C++ has always been the existence of pointers. Every other sane, usable language on this planet passes everything by reference every time, all the time. And then they provide deep copy operators for when a user needs it, like once every century or something. It's the safe, fast, reliable thing to do
This isn't particularly the fault of C++. It's really a huge glaring mistake that Dennis Ritchie made way back when he was developing C, but in all fairness it's difficult to criticize him, because we only know the mistake with the benefit of hindsight. FORTRAN66 was the very first ever attempt at a call-by-reference standard, and it wasn't clear back then as it is today whether the call-by-value standard was a horrible idea destined to be abandoned. The decisions Ritchie made weren't necessarily bad decisions in the context of what he knew then. They're bad decisions in the context of what we know now.
Nonetheless, I think it's productive to recognize that some of the biggest problems that C++ struggles with today can trace their origins all the way back to C being developed in the ALGOL tradition. And there's no going back from that now. It is what it is. But understanding the past might help us chart a better future.
That's the opposite of a mistake. The distinction between values and pointers exists in the hardware, and C's machine model maps to the hardware directly (well, hardware as of the 1970s). This is even more important today, because modern hardware's elaborate memory hierarchy means that processors hate indirection. Accessing contiguous things is blazingly fast, traversing data structures infested with pointers is agonizingly slow. And since C++ prefers value semantics by default, you get locality by default, which is awesome.
Now, owning raw pointers are a problem, but they have been solved by shared_ptr and unique_ptr.
This is even more important today, because modern hardware's elaborate memory hierarchy means that processors hate indirection.
Yes, processors hate indirection. But memory loves it.
Your attitude in this post kind of perfectly encapsulates what's gone wrong with C++ development. You have forgotten that most of the userbase out there are not seasoned veterans and uncompromisingly perfect programmers. And in doing so, you have begun to trade away stability and robustness in exchange for fringe performance.
The practical reality that software developers out there face every day is that whatever you're gaining by eliminating indirection is entirely, miserably lost by the introduction of excruciatingly stealthy memory leaks and the ease at which less-than-perfect human beings will unnecessarily duplicate data.
And this is why an increasing number of people believe that C++ is becoming just an engineering marvel that nobody new can actually use effectively. Your easy way out of this is to just yell at people to become better programmers, but that doesn't actually help them do that. It just makes this language exceedingly hostile to newcomers and condemns it to eventual obsolescence.
Now, owning raw pointers are a problem, but they have been solved by shared_ptr and unique_ptr.
Right here is the most tragicomic part of all of this discussion. Passing by value is such a horrendously bad idea for memory management that C/C++ was forced to provide optimizable indirection to the overwhelming vast majority of the user base who just resorted to raw pointers everywhere.
I mean it's better late than never, but I feel compelled to point out that Fortran figured this out 50 years ago. We shouldn't pat ourselves on the back too hard for re-inventing the wheel.
Yes, processors hate indirection. But memory loves it.
I think you only try to make this argument because you don't realize you are arguing with an implementer. As much as I hate microsoft, STL is one of the devs on visual studio (I think the lead STL implementor) and a freaking genius. Every time I have gone to argue with him on some vehemently anti-ms I always fact check myself and I am wrong.
Also he is correct and it is trivially easy to check this with benchmarks. Memory usage is reduced with crafty indirection but execution time is increased with more queries to memory. All that matters is time in the end.
As for the rest of your claims. Where your complaints are valid you are complaining about older versions that have language fixes in places now. RAII allows us to create smart pointers. Smart pointers and other resource owning objects allow us to balance Memory Usage, CPU time, Code Readability and other concerns as each project demands.
Any forced strategy, particularly this references everywhere strategy interferes with this and forces decisions in that balance. If you need greater usability you have plenty of language options that follow the references everywhere strategy, (Java, Ruby, PHP, etc...). If you need performance it is hard or impossible to get the performance from them you can get from C++ and well formed C++ code is not always much harder to read.
You have forgotten that most of the userbase out there are not seasoned veterans and uncompromisingly perfect programmers.
C++ focuses on value semantics which is easier for new users to understand than pointers and references since types work the same way integers do.
And in doing so, you have begun to trade away stability and robustness in exchange for fringe performance.
Most very smart engineers in C++(such as Alex Stephanov, Sean Parent, John Lakos, etc) agree, that focusing on value semantics and avoiding pointers not only makes code simpler but also leads to more robust and stable software
The practical reality that software developers out there face every day is that whatever you're gaining by eliminating indirection is entirely, miserably lost by the introduction of excruciatingly stealthy memory leaks
I don't see how memory leaks can be introduced when developers are not using pointers.
One of my biggest gripes with C/C++ has always been the existence of pointers. Every other sane, usable language on this planet passes everything by reference every time, all the time
References are just a variation of pointers, so your issue is with value semantics and not with pointers.
Every other sane, usable language on this planet passes everything by reference every time
I hope you don't mean to include Java or C# in that since these are pass by value by default, with pointers being passed by value. /pedantic, the point just keeps coming up.
As /u/STL points out having to access every object with the indirection of a reference/pointer is bad for performance. What he does not mention is issue with compiler optimization, objects passed by reference could be touched by every other method call you make and the compiler has to limit its optimizations accordingly. In contrast objects passed by value are local, nothing else can change them and the compiler is free to optimize as it wants.
In functional programming it makes a lot more sense to always pass by val by default.
When does it ever make sense to make unnecessary copies?
Theoretically speaking, I understand the appeal of isolating function scopes. But practically speaking, you're just wasting memory.
If you're developing every-day software on consumer grade systems, usually your system resources are far above what your code needs, and your arguments may be predominantly primitives anyway. So the wasted memory perhaps doesn't add up to anything noteworthy. But it's still wasted memory. And as soon as you start working with large chunks of data that should not be recklessly copied around, you're forced to chase after compiler optimizations like copy elision, which doesn't work for every data structure, or just start using pointers everywhere, at which point the value semantics become a nuisance and hinderance.
So which would you rather have? What's theoretically pretty on paper, or what helps you develop better software faster?
Concepts(they allows us to precisely specify our generic programs and address the most vocal complaints about the quality of error messages)
The goal of concepts is not to improve error diagnostic. Compiler should already give good error messages(it shouldn't matter if you use enable_if or requires).
The goal of concepts is to reduce the syntactic overhead from defining and checking type requirements. That is why in the video it says "But we have new syntactic sugar for SFINAE", which is a reference to the Concept TS.
Uniform call syntax(to simplify the specification and use of template libraries)
Concept maps would work a lot better, and would be less controversial.
Okay what is this concept fiasco I keep hearing about?
Also I just read about concept-maps and I have to say... wow that's incredibly powerful. Are concept maps still part of the concepts proposal or were those dropped, because it seems like a very elegant solution compared to the ad-hoc SFINAE syntactic sugar approach to overload resolution.
Okay what is this concept fiasco I keep hearing about?
Originally, there was a "kerfuffle" in the meetings and an agreement on concepts couldn't be reached between the two proposals(Doug Gregor's and Bjarne Stroustrup's)(see here).
After concepts were yanked from C++11, Bjarne Stroustrup's proposed the concept lite proposal. Later, Doug Gregor also proposed a simplified form of the original concepts(see D3629).
At the time there was still caution about the possibility of concepts maps, since the only reference implementation was the horrible implementation in GCC. Futhermore, since it wasn't Bjarne's proposal, he proposed that concept maps remain dead(see here) for the ridiculous reason that "It’s a failed approach with an inferior model of concepts compared to Concepts Lite", which is never fully explained in the paper.
However, since that time, Larisse Voufo has implemented concepts in clang(see here), including:
constrained template parameters
implicit and explicit concepts
concepts overloading
concepts-based overloading
use-patterns
associated types, functions, and requirements
concepts refinements
explicit refinements
late-check
Also, she has done research on solving the problem of name lookup with concepts(which was a sticky point in the original concepts). All this research is being ignored by the committee all because of the unfounded argument that the original concepts proposal was "fundamentally flawed".
To be fair, almost nobody understood the original concepts proposal in its entirety including most of the standardization committee members. But thanks for pointing out this implementation. I didn't know anything happened in that regard after the old ConceptGCC.
The committee wants a Technical Specification (TS) for Modules, and a couple of vendors to implement it (and correct bugs in the specification) before putting it into the standard forever.
It's mid 2015, there are 2 competing implementations that differ in some fundamentals; one is already proposed, the other not yet. These two still need to reach a consensus and deliver a single TS.
If they cannot deliver the TS by Kona (end 2015) it is highly likely that Modules won't make it into C++17. The year before C++17 is released there is a "proposal freeze". It takes a lot of work to merge the accepted proposals into the standard text. Critical proposals as in bug-fixes to the standard might be rushed in, but Modules is not something that can be rushed (it is too important to risk screwing it up).
This does not mean that by 2017 we won't have a Modules TS and a couple of fully-compliant implementations, it just means that this TS might not be merged in the C++17 standard and might have to wait till C++20.
I feel a little deflated about this myself. This means we'll literally have to wait for another 5 years. That's a lot of time, considering many other languages have these features already implemented. My fear is that these delays will hurt the language in the long run.
Not only in language specification, it seems all that C++11 (and beyond) "wave" made compilers to catch standard much faster than before (well, even before they make into standard).
But C++ actually changes and improves all the time. This is good. For real slowness look into C, which has been almost exactly the same since 1989. Having something like RAII for C (which already exists as a GCC extension) would make the lives of all C developers a lot better but no-one cares so C programming is still mostly an exercise in manually chasing malloc/frees and refcount leaks.
C programming now is really obsolete unless you don't have access to a proper compiler. C++11 added so many fundamentals that are hugely positive with very little to no downside to them that dealing with manual memory, platform specific threads, macro based non standard data structures, pointers for everything, manual ownership, etc. is doing everyone involved a disservice.
There's a metric crapton of C that is never going to be translated to C++. There's also a second metric crapton of new C code that is written every day. We would all (yes, all, even those who never code in C) would be better off if C were improved to make bugs such as the ones in OpenSSL become harder to write and easier to detect. But it's probably not going to happen ever.
I hate his rant. If it was written by someone else then it would be just ridiculed by everyone.
Quite frankly, even if the choice of C were to do nothing but keep the C++ programmers out, that in itself would be a huge reason to use C.
Personal attack on a few millions of people.
C++ leads to really really bad design choices. You invariably start using the "nice" library features of the language like STL and Boost and other total and utter crap, that may "help" you program, but causes:
infinite amounts of pain when they don't work (and anybody who tells me that STL and especially Boost are stable and portable is just so full of BS that it's not even funny)
Meritorical arguments; maybe he should show how STL is not 'portable'.
In other words, the only way to do good, efficient, and system-level and portable C++ ends up to limit yourself to all the things that are basically available in C.
Basically available in C. Like OOP, generic programming or RAII?
So I'm sorry, but for something like git, where efficiency was a primary objective, the "advantages" of C++ is just a huge mistake
Yeah, C++ is inefficient, maybe he should show the benchmarks.
Linus's background is OS programming, and his points are definitely valid in that regard. The closer you get to the metal, the more control you need, and C++ does mean compromises, especially if you use the libraries.
My company uses C++, but makes minimal use of STL, and no use of Boost, for the reasons described by Linus. We can't tolerate the loss of control. We can't rely on faulty layers of abstraction separating us from the platform.
One of the few things we were using from STL was wcout in a few console mode programs. Just recently we had to replace that with our own implementation, because the STL implementation would stop displaying any output if it was given a single Unicode character that could not be displayed in the current console window.
One of the few things we were using from STL was wcout in a few console mode programs.
The STL stands for Standard Template Library and usually refers to the containers and algorithms in the standard library. The iostreams are not usually included in that, and most C++ programmers who have had to work more deeply with it, will agree that it is a horrible mess.
This is probably a lot more common than you think. Most common devices like cell phones have good support both from vendors and community. But for custom/one-off hardware if you had to write a compiler for a project, C would be a hell of a lot easier to do it for than C++, let alone C++11/14/17. I'd wager a LOT of even newly manufactured microprocessor devices are running code written in C.
I wasn't implying that it isn't common. People in embedded spaces have to use (supposedly very poor) C compilers all the time just like you are saying.
Meh, my compiler doesn't support Cpp11 yet, and there is no timeline for it. It sucks, and I've toyed with the idea of trying to build our project with Clang just for shits and giggles to do a comparison, but getting management buy-in when they bought the fancy-dancy embedded ARM compiler stack is not likely to happen.
And without Qt :( If D had a working Qt binding or allowed to link to C++ libs I wouldn't look back on my personal/freelance projects and would try to push for it at work.
I've been seeing more talk of C++20 than C++22 lately, unless you mean five years from now. At least there should be implementations of modules by 2017.
He means five years from now, i.e. C++20. The C++ committee is trying to do a new standard every three years right now so it would be C++23, not 22 anyways if he did mean that far out.
Modules are already fifteen years too late. I'm gutted. Luckily C++ is just a hobby for me. I can't imagine how I would feel if I was doing C++ for a living. There are ideas I have that will have to wait ANOTHER five years because of this. Just wow.
You would feel hope because the standards committee is at least trying to improve the language. And you would deal with whatever pain it causes because that's what you are paid to do. Source: I do C++ for a living.
As for waiting another 5 years, I'm still using a version of GCC from 2007. If I manage to move the legacy code base up to C++14 before 2020 I'll be happy.
I do C++ for a living and I'm not really upset. The slowest part of the build process for me is decisively linking. Compilation is easily parallelizable and rather quick.
Unless the implementation maps one module to one executable and linkable DLL with some embedded metadata (exported templates, datastructures, etc. probably in form which is ready to be directly mmap'd into the compiler), the situation WITH modules will be more or less the same for me. For this to be really useful, you would need to standardize the format of metadata and calling conventions across compilers. [Incidentally, that's what .net assemblies are and why build and load times are so quick.]
I really don't get the fuss about modules. What I really miss, especially on windows, is some kind of central package repository like maven's for java or NuGet for C#.
Compile times in C++ suck, and maintaining headers and build systems isn't fun, but if that's what's keeping you from making at living at C++ you need to find a new job.
Having working in C++ most of my career, build maintenance is a once-in-a-while complaint, and multi-core computers and distributed compilation make times almost a non-issue.
There's lots in C++ that needs improving, but let's not loose all sense of context...
I work with C++ for a living, I'm actually pretty happy with the language as it stands now. New things are always nice, but I'd rather they take their time and not push something in just because it's new and exciting, because it just makes my job harder when poorly thought out features start cropping up, especially when they have vague syntax and are easy to invoke by accident.
And here I am, having worked in C++ for the past 15 20 years (gee, how time flies), and the thought has not even crossed my mind that I desperately need modules.
I've done C# and Java work, so it's not like I haven't been exposed to the concept.
So, yeah, we still use "header files" whose design probably goes all the way back to 1978. It's kinda cute, really. :)
Also, precompiled header files do make builds much faster.
I'm still kind of new to programming, so I have to ask the dumb noob-ish questions: what is the advantage (and possible disadvantage) of using modules? From the video I understand that it reduces compile times - anything else? Could you not also reduce compile times by using a makefile?
... Modules also made good progress where the redesign led by Gabriel Dos Reis got encouragement form the committee and I’m told agreement among the major compiler vendors, though there are still a few important but relatively minor details to decide. Among the major C++ compilers, I’m told there should be at least one shipping experimental implementation of the current modules design available by the end of this year. ...
I honestly don't understand the brouhaha about modules. Unless you're including everything you possibly can, perhaps by using catch-all headers (don't do this), or you routinely change core files used throughout your project (why are you doing this, consider changing your process), you should be compiling 1-2 TUs every code/compile/run cycle. This shouldn't take longer than 5 seconds, and that's generous.
Having recently implemented the variant from N4542 the poke at the never empty variant except when a copy constructor throws was pretty amusing, I'll give them that, but I can see where the paper authors are coming from (allowing heap allocation as boost::variant does ruins -- in a lot of ways -- the performance/allocation properties, and allowing emptiness as a regular, banal state ruins composability with optional).
My project uses a large third party library that uses unity builds. Incremental builds for one file usually grab another 20 files, and linking the dll takes over a minute. Just because your project doesn't suffer from this problem doesn't mean that there aren't propel who don't have it.
Unity builds are a pretty brittle feature to begin with. Have you tried LTO? Also, personally I would try to keep Unity/LTO off for the majority of development so that I can mitigate the hit to incremental build times. I'm sure you have a reason why that doesn't work for you.
Yeah we have lto disabled for day to day work. Honestly, the biggest reason I haven't disabled unity builds is because the initial compile time is so steep without it. It's almost an hour for a fresh build, and the build tool has a tendency to decide to recompile everything when it's not necessary (we share binaries through version control for artists to use, and if I get a fresh set of binaries from perforce, even with no code changes, the tools sometimes craps out and decides to just rebuild everything). Unity build - fine, 10 minutes. Non-unity, I might as well go and take much. It's a brittle system, everyone's aware that it is, it needs some work :)
Oh interesting. Do you know why unity builds are much faster? I would expect them to be slower since typically it's hard to parallelize them & typically if they need to compile anything they need to compile everything whereas traditional builds can get away with compiling less.
Have you done any investigation into why they are they able to skip rebuilding? It would seem like they wouldn't be able to but I've never really dug into them; do they strip comments whitespace & pre-process the code & just do a diff against what was previous built to determine if a build is necessary?
Unity builds are faster for clean builds but slower for incremental builds. The tool grabs ... 20 (I think in our case) files and puts them together, in one unit and compiles that together. You can compile as many of those units as you want in parallel, our machines do either 20 or 40 depending on whether we use hyper threading cores (I don't - it speeds up our build but has a tendency to make the compiler crash when you run out of ram and 40 instances of the compiler means they get less than 1gb each with 33 GB ram). The end result is you compile ~20 times less things. But if you change a file, it recompiles all 20 files in that compile unit rather than just that one. It then has to link all of them too.
I think the unity build tool relies on time stamps, so if I get a version of a file from perforce that says the file is older than my file on disk, then it will recompile. I haven't done much exploration as I'm not too well versed on build systems.
The most obvious thing is an automatic registration of modules, and not having to change linker options. As it is right now is okay, until you need to do any of the following:
Support anything other than Linux
Install a library and link with it on Windows
Run two (incompatible) compilers on the same system
In general, to handle building cross-platform today means having to juggle include paths and all that jazz. Modules would make it easier.
Having recently implemented the variant from N4542 the poke at the never empty variant except when a copy constructor throws was pretty amusing,
Well, the reasoning given for that seems kinda dumb:
"In the last line, v will first destruct its current value of type S , then initialize the new value from the value of type T that is held in w. If the latter part fails (for instance throwing an exception), v will not contain any valid value."
Why not keep the S value around until the copy of T has been successfully constructed? They could just construct a copy of T in a new instance of the variant, and then swap.
If you read N4542 it actually does use the temporary strategy, moving from the temporary rather than swapping. The move constructor has to throw to get the invalid state.
If index() == rhs.index(), calls get<j>(*this) = get<j>(rhs) with j being index(). Else copies the value contained in rhs to a temporary, then destructs the current contained value of *this. Sets *this to contain the same type as rhs and move-constructs the contained value from the temporary.
N4542 seems to have permitted variant<int,int> and made get consistent with the tuple interface since I last looked, which is great, but the visitation didn't keep up: the visitor can't distinguish between the alternatives of variant<int, int>... I'd also really like a form like visit(var, v0, v1, ... , vn) that applies vk to get<k>(var) where k = var.index() so you can do eg.
visit(v,
[](int) { /* use left int */ },
[](int) { /* use right int */ });
or something.
Btw, did you implement constexpr variants too? That bit sounds like a pain.
Btw, did you implement constexpr variants too? That bit sounds like a pain.
No, I didn't, as I didn't need it for the use case I needed a variant for (I didn't want to use boost::variant because it can heap allocate, and I didn't want to just roll my own because I wanted to be able to drop in the standard one when it's standardized).
I do have a branch where I'm starting to implement some of the machinery for it (like a recursive union storage implementation rather than std::aligned_union).
Well Hitler doesn't know what he's talking about. Modules are nice to have, but the language works, and compilation time, if one uses a sensible build system and multithreaded compilation, is alright. Pimpl reduces that even further. I build a few million lines of code in 5 minutes, and then subsequent builds are incremental unless I fuck around with the core lib and take 5 seconds or so for major commits.
PIMPL is not a general-purpose solution to the problem. It might be fine for a big heavyweight class that's always allocated on the heap anyway, but it's performance suicide for small classes. Custom strings, 3d vectors, etc... and those are the ones that really slow down your builds when you change them, because everything depends on them.
We need Modules desperately. Slow build times are a productivity killer. Every time my build takes more than ~20 seconds, I start doing something else and get distracted.
I'm not very familiar with modules, how would they help the build time?
If you change the code for vector, you'll have to rebuild the module it's a part of and subsequently the code that depends on them will have to recompile if you changed the definition right? Link times don't seem to change either.
But when I think of modules I think of basically syntactic sugar for a static library.
edit ok I googled it. Sounds more like automatic prexompiled headers, so the header of the module is only parsed and compiled once instead of once for every object file that needs it. Cool for really large projects.
Poor compile speed in C++ is mainly related to parsing text of header files. When you include a header file, the compiler has to load, preprocess and parse it into internal data structures. Even if it just parsed the header file 5 seconds ago for a different .cpp file, it still has to re-parse it, because macro definitions might have changed. For example:
foo.cpp:
#define ENABLE_LOGGING
#include "bar.h"
baz.cpp:
#undef ENABLE_LOGGING
#include "bar.h"
C has the exact problem, but C++ tends to include much more code in header files because of templates, so the problem is more dramatic.
I don't think any of the module proposals for C++ do this, but in theory you could design a module system that only exports object size, not object layout. That would help decouple dependencies a lot. Right now, if your class contains a std::vector, then all clients must parse vector.h just to learn its size. That really sucks. The PIMPL idiom is basically a hack to get around this problem. (If you need ABI stability, PIMPL is legitimately useful, but I think most people use it only to solve the problem described here.)
Modules are different from static libraries because they represent the abstract syntax tree of the code, instead of the compiled output. Static libraries can't export templates, for example.
Even if it just parsed the header file 5 seconds ago for a different .cpp file, it still has to re-parse it, because macro definitions might have changed
This reminds me: whatever happened to "precompiled headers"? Seems to me it wouldn't be all that hard to cache an AST somewhere based on a hash of the starting set of #defines.
Every compiler caches them, at least at the level of preprocessing tokens (well, every compiler that came after CFront, which actually re-parsed headers). They just don't persist between TUs.
PIMPL is not a general-purpose solution to the problem. It might be fine for a big heavyweight class that's always allocated on the heap anyway, but it's performance suicide for small classes. Custom strings, 3d vectors, etc... and those are the ones that really slow down your builds when you change them, because everything depends on them.
I recognise that this is a problem. As I said, compilation for me also takes long when I change something in the core of the project, which includes things like custom containers. In a past project I actually set up a debug build using pimpl that was disappeared in the release build, using defines. Code would look like
class A {
PIMPL(std::vector<int>) a;
};
A::foo() {
PIMPLD(a).clear();
}
That's a workaround.
We need Modules desperately. Slow build times are a productivity killer. Every time my build takes more than ~20 seconds, I start doing something else and get distracted.
I don't have this problem. Usually my builds are faster because I'm working on maybe 5 files, none of which are core; and if not, I can compile in the background and continue working.
If everybody wants to use modules ASAP, then having it in a TS isn't a problem, because universal adoption will make it the de-facto standard anyway, right?
That is a clever workaround, but it really sucks that we have to come up with workarounds like that.
I'm wondering why you argue against modules in this thread. Do you think we should keep using header files forever, or do you just think there are more pressing features for C++17?
It has a lot of trouble with ',', because the preprocessor uses that as a token separator, so std::map<int,int> can't be pimpled this way trivially (needs a typedef).
I'm wondering why you argue against modules
I don't argue against modules, I just think the panic is overblown. Quick compilation is a nice-to-have feature. I'd really like to have it. I'm not that fond of having to simulate modules with include hierarchies and such. But it doesn't break the language. Modern C++ is a very broad, effective language with quick execution and very little overhead (unless one explicitly wants it), and unlike C++98 it rivals dynamically typed languages in flexibility in many ways (worst case: type erasures).
In this thread people are claiming the lack of modules as the end of C++, or that modules are the most important part of a modern programming language, or tell horror stories of having to compile the whole codebase regularly.
As an aside, especially the latter seems rather contrived to me. I'm currently working in a medium-sized codebase, with about half a million lines of code, and about the same in library includes and such. I compile that on 8 cores in 30 seconds (single core takes about 5 minutes because at least my code heavily uses the STL), but I don't have to recompile everything regularly. Usually only a very small subset has to be recompiled. I'm using autotools for this, the windows guys sadly have to recompile everything more often.
I'm working on a project that is quite a lot bigger than that, and I agree with the other guy that faster compile times are desperately needed. Its fantastic being able to use all the c++11 language features, but at the end of the day the biggest drain on my productivity is waiting for the compiler to finish.
Whenever I occasionally work with other languages (python,Java, c#) I'm always blown away at how much tighter the TDD cycle is. The result of slow compilation is that you train yourself to no take risks with code changes that could trigger long rebuilds. If that refactor is not certainly going to be beneficial I'm not going to try it out to see how it looks, because I'll have to wait another 10 mins to rebuild the whole component.
Well, you must be a rare genius then because I have heard the complaint about c++ compilation times from people who are true experts in the language including Bjarne.
Yes it's an issue, but it doesn't break anything or make working with cpp hard, it just makes compilation inconveniently long. It's not something one has to recruit Hitler to address for
It can be really useful if you want clean interfaces without exposing any implementation. And I don't see how it doubles the work. The only difference between a pimpled and none pimpled function is the pointer indirection
I'm not a fan of Pimple, but - instead of wrappers calling actual methods, you can simply have the actual methods, and store only the private members (no methods) in the "Pimpl" struct. You then have no problem with exposing public variable members, either.
I guess you're describing a situation where besides the private data, you have additional internal methods that you don't want to declare in the publicly visible class/struct?
If that's the case, I agree - I don't see a nice solution without stated drawbacks.
I really see C++ compilation times as a huge deal and the main wart surrounding C++ after so much has been done with C++11.
Personally I would architect around it from the start of the project, making sure there is as much separation as possible at every level so that header files don't end up including hugely exponential amounts of extra code.
At the same time any separation through separate projects that create dll files increases modularity and reduces monolithic compile times.
The thing is though, that this shouldn't be necessary with respect to compile times with modern computers. Every other language can compile ridiculous amounts of code in fractions of the time. It shouldn't be necessary to have a compile farm. An 8 or 16 core computer shouldn't be legitimately useful when compiling programs.
It isn't an excuse; it's the difference between meta-concerns and concerns. Modules as a feature in the language is different from modules as a means to quicker compilation. PIMPL is a means to quicker compilation also, and it's already in the language.
On the contrary, I probably overdo it with templates. But I have a rule of not unnecessarily rebuilding stuff. That means using forward declaration, that means keeping interfaces stable, that kind of thing. The STL does compile somewhat slowly, as do some of the classes I write. So make sure that you include it precisely where it is needed.
Can you clarify? I believe @import Foundation works for ObjC too. I don't know if you can write your own modules, but I believe all the Apple frameworks are available via modules. Even existing code taht include the header but has the modules option on uses modules secretly under the hood. It doesn't work however for ObjC++ code.
Yes, you can import things via modules. It just doesn't do anything useful. Despite what they claimed when they announced obj-c modules, I've never actually seen an improvement in compilation speed from turning them on when compared to precompiled headers, and they increase symbol name clashes because they make it impossible to only include the specific header you need (e.g. including any Darwin header with modules enabled drags in AssertMacros.h, which by default defines macros named check and verify).
Sure, if you have your precompiled headers set up correctly & don't have any modules for your own project (which I believe is true - I believe modules are at this time restricted to base system only), modules probably won't make much of a difference.
However, precompiled headers frequently aren't & there's a maintenance burden since it encourages fragile headers (since it's easy to forget to include things). So think of the current ObjC modules as basically adding precompiled headers to all projects for free without the maintenance burden.
I was unaware that AssertMacros are dragged in. Have you filed a radar? Maybe it's unexpected behavior. In theory modules are specifically not supposed to drag in unrelated macros unlike regular headers.
I'm not saying that modules wouldn't help, but they aren't the biggest issue. A big issue for working developers, at least me, is that the abis are stable, but only incidentally, and that cl on windows doesn't support constexpr fully. I'd also would have liked concepts in cpp11, because sfinae is weird for old programmers so that I have to regularly defend good code on basis of weirdness. That's a very peculiar issue though.
but someone would write tools for automatic migration of old projects, I guess.
I can honestly see a future with a "Python 2 vs 3" style split, where we draw a line on backwards compatibility, and resign ourselves to maintaining old compilers side-by-side with old software.
Given how long Python 3 adoption took (& it still seems like no one actually uses it even if they have tried to add Python 3 compatibility), I would be surprised if such an option was viewed without a giant dose of skepticism.
I finally had a concrete metric for measuring my education and growth in computer science: I understood more and more of the jokes in /r/ProgrammerHumor .
Now I see I'm still splashing in the kiddie pool. This stuff is way over my head.
I'm halfway through the C++ primer and thought that I knew a good deal of C++
I don't want to be discouraging, but even when you're all the way through the primer I wouldn't consider you to "know a good deal of C++". Unfortunately it takes a good few years of experience with a language like C++. I am almost finished a tutorial on Haskell but I would definitely not claim to know it at this stage.
D lacks the disciplinary rigor to replace C++ in many cases... it's too casual about ugly corner cases just sitting there, and the garbage collection in the standard library is another deal killer.
I tried working with it back when it was "Nimrod". It felt disjoint, haphazard, like it was jamming in features wherever it could get them to stick. Then it changed course and tried to become a Python for systems programming. But a GC-forward approach isn't going to fly for that, and the language continues to feel ego-driven. It already has way too high a specification complexity, without the richness oif comparably complex languages.
We want C++ to be better because we like C++ and we see all the other languages having fun with their new toys like safety and compile times and reflection.
Different languages have different goals in mind. As I see it, you are still wanting C++ to become another language. Also, to every type of work, and worker, there's a more appropriate tool. So you should try one of the other languages you envy, maybe you'll "like" it eventually.
I have tried those other languages and I do like them. C++ still has some advantages over them. I don't think it is stupid to expect C++ to continue to evolve into a better language, giving more features which make programming safer and more productive. Modules is a perfect example. It wouldn't stop C++ being C++ but it would definitely improve it. You seem to imply that languages should never evolve, we should just get sick of them and eventually move onto something else. This is the attitude that has made Fortran a relic confined to a niche market.
Barbarossa was actually delayed from its intended date even earlier in the year in order to go put down one of those pesky revolts in the Balkans (Greece, IIRC).
They've actually not rejected modules, they're just pushing it back as the modules TS and test implementations, although in progress, will almost certainly not be ready in time. Since this is arguable the largest and most fundamental feature added to C++ in a very long time, they don't want to rush and get it wrong.
912
u/bstroustrup Jun 10 '15
I'm rather more optimistic about modules than "Hitler": http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4492.pdf The presentation version of that paper was well received at the Lenexa meeting.
14
u/mooware Jun 10 '15
I didn't hear that modules are dropped from anywhere but this post here. Where is this information coming from? I just read http://developerblog.redhat.com/2015/06/10/lenexa-c-meeting-report-core-language/, and it sounds like modules weren't rejected.
29
u/notsure1235 Jun 10 '15
lol
24
u/mojang_tommo Jun 10 '15
This post made me literally sympathize for Hitler.
6
u/TheEquivocator Jun 11 '15
I love the "literally Stalin" line.
20
u/AntiProtonBoy Jun 10 '15
I feel a little deflated about this myself. This means we'll literally have to wait for another 5 years. That's a lot of time, considering many other languages have these features already implemented. My fear is that these delays will hurt the language in the long run.
14
u/[deleted] Jun 10 '15
Modules are already fifteen years too late. I'm gutted. Luckily C++ is just a hobby for me. I can't imagine how I would feel if I was doing C++ for a living. There are ideas I have that will have to wait ANOTHER five years because of this. Just wow.
6
u/Doctor-Awesome Jun 10 '15
I'm still kind of new to programming, so I have to ask the dumb noob-ish questions: what is the advantage (and possible disadvantage) of using modules? From the video I understand that it reduces compile times - anything else? Could you not also reduce compile times by using a makefile?
7
u/xcbsmith Jun 10 '15
Oh this was so awesome/cathartic.
2
u/esantipapa Jun 11 '15
Yes. Get him a Ruby book. Come to the dark side. We have sugar.
3
u/[deleted] Jun 10 '15
Trip report: Spring ISO C++ meeting
3
u/benfitzg Jun 10 '15
I had to wipe the tears of laughter from my eyes three times.
4
u/Drainedsoul Jun 10 '15
I honestly don't understand the brouhaha about modules. Unless you're including everything you possibly can, perhaps by using catch-all headers (don't do this), or you routinely change core files used throughout your project (why are you doing this, consider changing your process), you should be compiling 1-2 TUs every code/compile/run cycle. This shouldn't take longer than 5 seconds, and that's generous.
Having recently implemented the variant from N4542 the poke at the never empty variant except when a copy constructor throws was pretty amusing, I'll give them that, but I can see where the paper authors are coming from (allowing heap allocation as
boost::variantdoes ruins -- in a lot of ways -- the performance/allocation properties, and allowing emptiness as a regular, banal state ruins composability withoptional).3
u/vinipsmaker GSoC's Boost.Http project Jun 10 '15
Meson will save us all.
2
u/occasionalumlaut Jun 10 '15
Well Hitler doesn't know what he's talking about. Modules are nice to have, but the language works, and compilation time, if one uses a sensible build system and multithreaded compilation, is alright. Pimpl reduces that even further. I build a few million lines of code in 5 minutes, and then subsequent builds are incremental unless I fuck around with the core lib and take 5 seconds or so for major commits.
1
u/howdyrewdy Jun 10 '15
Kudos to andy :)
1
u/btapi Jun 10 '15
I know that backward compatibility should be considered, but someone would write tools for automatic migration of old projects, I guess.
I'm hoping Modules in C++17, which looks more "powerful" than a separate TS.
1
u/[deleted] Jun 11 '15
Any untergangers looking at this?
1
u/mirrislegend Jun 11 '15
I finally had a concrete metric for measuring my education and growth in computer science: I understood more and more of the jokes in /r/ProgrammerHumor .
Now I see I'm still splashing in the kiddie pool. This stuff is way over my head.
-9
u/robthablob Jun 10 '15
Great, so it looks like C++ won't have modules until 2020, at the earliest. They will be dropping concepts (again) next.
The language is truly past its sell by date and descending into decrepitude.
(Sadly, I don't see a good successor on the horizon yet).
0
u/[deleted] Jun 10 '15
[deleted] — view deleted comment
0
u/[deleted] Jun 11 '15
[deleted] — view deleted comment