by deutschepost · 3y ago · 0 comments
But in working with c++ day to day I feel the most annoying things are indeed dealing with dependencies and headers. It can be a pain to set up a complex c++ project on a new machine. Even with Conan it can still be a pain because configuration management is a mess in c++.
As for headers I can’t wait to see them rot in hell. Some people say that they can help you reason about your Program, but this is a very small positive point. Too many times I had problems with includes which only worked because a Translation Unit included some other header beforehand. I once read a blogpost of the developer of SumatraPDF where they describe that they never include anything directly[1]. This can be an improvement for compile time. But if someone else is working on your code or has to refactor it, it can be impossible to read yourself into it or track any compile time error. If one would only use modules these errors would simply not exist.
[1]https://blog.kowalczyk.info/article/96a4706ec8e44bc4b0bafda2...
$ gcc -E hello.c | cloc --force-lang=c -
419
$ g++ -E -std=c++11 hello_iostream.cpp | cloc --force-lang=c++ -
20707
$ g++ -E -std=c++20 hello_iostream.cpp | cloc --force-lang=c++ -
30876
$ g++ -E -std=c++20 hello_std_format.cpp | cloc --force-lang=c++ -
42757
Just bumping the C++ std version can add thousands of lines of code.And yet none of this heavily influences compile times. At least I have not found so on MM LoC projects I've been working so far. Not sure what the fuss is around modules, I honestly don't think it will solve C++ build times. Happy to be proved otherwise.
Given that modules are PCH in disguise, I don't believe modules are going to have a dramatically bigger, and better, effect on C++ build times. Whether or not they are going to cut the build time from 45 seconds to 30 seconds I also couldn't care less - small projects don't even have the build time problem worth solving. Mid and big projects is where it will really count and if modules are going to have a substantial effect in the range of at least 20-30% in build time cut, I will be all ears.
It's a shame they add so much header bloat, but there's a lot of functionality in there and it's hard to see a way it could have been avoided if you want type safety.
As for the header bloat, fear not, import std; for the complete C++ standard library, is faster than a plain #include <iostream>, as per VC++ team measurements.
Oh, sure, you can manipulate this by inserting magic formatting objects, but that's not different from saying all functions take one argument a la Category Theory or Haskell without syntactic sugar. Or you could write a class or function that allows you to tag your object or value by changing its type to a different one so a different operator<< overload is called. In the iostreams design, you're only indirectly selecting which print function to use by manipulating either the stream or the printed object. But in actuality the types of your two arguments are not different, so either you must munge global settings on the stream or "hack" which operator<< overload is selected.
And that's the crux of the issue; what you "really want" is a stream API with three arguments: the stream, the object or value, and a pointer to a function (or function object) that takes the value and transforms it to a stream of characters. Why not just pass that function directly and explicitly? Of course that requires something that's not just a single chained infix operator; my thesis being that starting with that quirky choice leads to down a path that ends in a bad design.
So it's not a story of "abandoning", more like an insane C idiosyncrasy that wasn't ever used anywhere else.
They do not work well C++ though because it puts the implementation into the header for some reason.
import x
class Foo:
def bar(self):
import y # break circular import
y.something()
Would be nice to be able to import `Foo` without pulling in also `y`, or moving `y` inline.Can be solved in different ways, but you see inline imports everywhere.
Almost every other module-based language does not have issues with circular dependencies. Python, in theory, could follow their lead, but they won't.
EDIT: I wasn't as clear as I could be. The issue again isn't modules, but scripting languages that allow top-level statements at all. Intermixing types and method declarations with executable code makes circular decencies an issue. Compiled languages don't allow top-level statements, so they don't have the dependency resolution problem.
But from a developer user experience standpoint, what is the difference between having to write things twice for this reason:
// header.h
int foo();
// library.c
int foo() { return 42; }
Versus writing things twice for this reason:
// interface.cs
interface IBar { int foo(); }
// library.cs
class Bar : IBar { int foo() { return 42; } }
Granted in C it was for dumb reasons whereas in a modern language it's for better (?) reasons, but: You're still writing things twice!
Not in Oberon.
Expose the types, and the public procedures. Users only need to see the spec during compilation.
Hence name mangling, the only way to add some linking type safety, while using a bare bones UNIX linker that only knows C and Assembly.
On the flip side, I think there are still a lot of developers who don't use language servers or use the equivalent of notepad who do rely on separate header files as a means of documentation.
That said, it's always been painful to structure inter-related headers and source files to avoid circular dependencies, and if modules truly resolves that, I'm happy to see it.
In exchange for reducing parallelism because you are not using forward declerations to break dependency chains up.
The big difference is that those communities embrace compiler and tooling are part of the same story.
Thankfully now we have a tools working group trying to bring C++ community into the modern world of module based compiler toolchains.
Go, compiling code in parallel since Go 1.19. One is able to compile the whole toolchain from scratch faster than many C++ codebases.
Active Oberon, compiling in parallel via the Paco toolchain since 2003, a full graphical workstation OS, built in a couple of minutes.
Ada, parallel compilation available in most toolchains since 1989. Also available in GNAT.
Delphi, yet another example.
It is C++ that needs to get up to date with modern toolchains and away from hacks like unity builds.
I once refactored part of my code for headers to only #include forward declarations (and few generics, of course), except where really needed (implementation)
It made compile times 2x faster, probably could get 3x if fully done.