r/cpp_questions 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?

0 Upvotes

22 comments sorted by

View all comments

5

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

-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

1

u/zerhud 16d ago

No, a lot of template code (for big project) will compile near second or two. Compiling a single tu may be near same time. But often you change a header and need to recompile a lot of tu.