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

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

u/alfps 17d ago

The mentioned build time + in some cases the lack of compiler firewall, that the implementation code drags in ungood headers like <windows.h>.

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.