Should I be using C++ modules instead of #include?
Asked Answered
I

0

6

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.

Incalescent answered 15/1, 2022 at 7:34 Comment(9)
According to cppreference, no compiler other than MSVC advertises full module support, so it's too early to switch to them.Hootman
Here is a list of good C++ books 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.Cooley
Yeah @HolyBlackCat, it seems that I did. ThanksIncalescent
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)Incalescent
You'll need to know both anyway.Hootman
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.Hudgens
@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 : isocpp.github.io/CppCoreGuidelines/CppCoreGuidelinesReasonless
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 #includeMejia
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!Incalescent

© 2022 - 2024 — McMap. All rights reserved.