3

I'm a new C++ learner, and I've been thought to use #include and header files to import files from other libraries or files (such as the stdlib). However, I recently came across "modules" in C++ 20. Should I be using modules instead of #include?

Furthermore, if I should be using modules, are there still benefits to using a .h file + header guard + .cpp file instead of just importing a module from a .cpp file? The only benefit I can think off the top of my head is abstraction.

Cloudate9
  • 91
  • 1
  • 7
  • 2
    According to [cppreference](https://en.cppreference.com/w/cpp/compiler_support), no compiler other than MSVC advertises full module support, so it's too early to switch to them. – HolyBlackCat Jan 15 '22 at 07:38
  • Here is a [list of good C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) that you can/should refer to. First start by clearing your basics. Then after you know how to use header(.h) and source files(.cpp) correctly move onto modules. – Jason Jan 15 '22 at 07:38
  • Yeah @HolyBlackCat, it seems that I did. Thanks – Cloudate9 Jan 15 '22 at 07:39
  • Yeah @AnoopRana, I'm still in the process of learning CPP. I wanted to clarify though, so that I start with good habits (I've been down the rabbit hole of fixing bad habits, that was horrible) – Cloudate9 Jan 15 '22 at 07:41
  • 3
    You'll need to know both anyway. – HolyBlackCat Jan 15 '22 at 07:47
  • 1
    If you are new then definitely avoid trying to learn bleeding edge features that are weakly supported and going to change. Compile your code in C++14 or C++17 mode. – Öö Tiib Jan 15 '22 at 08:43
  • 1
    @CloudOn9 Oh I like that attitude! And yes modules might be a bit too soon, but if you want to pickup some other good habits have a look at this : https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines – Pepijn Kramer Jan 15 '22 at 09:00
  • 2
    it will take some time for modules to become the defacto standard of organizing projects and even then there will be tons of older libraries and projects that don't use them. In other words, its definitely no lost effort to get accustomed with `#include` – 463035818_is_not_an_ai Jan 15 '22 at 14:33
  • Thank you for the comments everyone! I'll be using `#include` and header guards for now, and maybe look into modules again once C++ 23 comes out. Thank you all! – Cloudate9 Jan 15 '22 at 18:40

0 Answers0