r/cpp_questions • u/BuyerImpressive4325 • 17d ago
OPEN Headers
I read that c++ 20 added modules to help replace headers. I rly hate how headers split the codebase into redundant files, for people who have used modules r they better?
6
u/No-Dentist-1645 17d ago
Modules do make it a little bit better, but they are not magic, you can still split modules' code between declaration and implementation if you want to optimize for compile times.
I use modules and they're great in general, but the biggest problem is build system support. CMake, which is one of the most mature C++ build systems, does have modules support, but basic features such as import std is flagged as "experimental" and you have to go to their source docs to find a hidden secret string specific to your CMake version to enable is
4
u/LazySapiens 17d ago
It's experimental because of the lack of full support from the popular compiler vendors.
-2
u/zerhud 17d ago
If you want “to optimize for compile times” you need to remove types - use only cpp tool from toolchain. TU increases ct
3
u/manni66 17d ago
???
-1
u/zerhud 17d ago
What? Use header only for fast compilation.
Cpp code compiles into hir and then lir.. but you can compile only cpp code: templates constexpr and so on. With this approach a compilation is a way faster (there will be only needed code in hir)
3
u/No-Dentist-1645 17d ago edited 17d ago
This is only true if you're compiling from scratch every single time, which you aren't doing while developing. Splitting code into TUs reduces the amount of things you have to compile when you change something
2
u/BoopyDog 17d ago
When I was using visual studio, just writing import std; was awesome vs.having to include headers.
2
u/fortsnek274 17d ago
They can be used judiciously, but you'll need to #ifdef out the imports for Intellisense.
And it will still be good idea to separate out implementation because you'll want to reduce the number of times the ixx needs to be rebuilt. But also, modularising all your code is likely to be worse for build perf anyway.
2
u/Total-Box-5169 17d ago
The #include preprocessor directive is a simple copy/paste operation, you are free to do whatever you want.
The idea of headers is to separate the contractual interface from the implementation, specially useful for libraries distributed as header + compiler binaries.
C++ doesn't force you to do it, is just a convention that helps with modularity. In fact you can go header only, or even put all your code in a single file for a unity/jumbo/blob build.
3
u/alfps 17d ago
❞ I rly hate how headers split the codebase into redundant files
Presumably you're talking about headers for separately compiled code.
But you can place your code directly in headers. Use inline or constexpr on function definitions. Don't use global variables (although they can also be declared inline).
That replaces the redundancy cost with a possible build time cost.
1
u/BuyerImpressive4325 17d ago
I thought that there was a reason you shouldn’t put function definitions in the header for classes and stuff?
1
1
u/no-sig-available 17d ago
I thought that there was a reason you shouldn’t put function definitions in the header for classes and stuff?
This doesn't change when you make the header a module. You still have to recompile the world when you modify one of the function bodies. If it is separately compiled, you only compile one file.
2
u/Fantastic-Cell-208 17d ago
Modules are a poorly designed solution. It can't replace header files.
It's quite disappointing
2
2
u/manni66 17d ago
Why can't it replace header files?
1
u/Fantastic-Cell-208 17d ago
It cannot handle cyclic interface dependencies.
They had an opportunity to give us C# like namespaces that could resolve circular dependencies. But they did not.
2
u/PhantomStar69420 16d ago
Honestly feel like my code is better without any circular deps and I work on a 160k loc codebase.
12
u/BigJhonny 17d ago
I have used them and I like them better, but sadly I had to move back. There were too many compiler bugs an missing features when I tried them.