4

This question might be too broad but it really tickled my mind lately:

I recently found out about modules in modern C++ : https://en.cppreference.com/w/cpp/language/modules

But I really don't get their purpose and when to use it instead of namespaces or just library headers?

In the example provided they now use import <iostream>; instead of include <iostream>;, what is the the difference in using one versus the other? Which one is to be preferred?

They say that "module are orthogonal to namespaces"? What does this mean?

What are the guidelines regarding development, should we now avoid headers and stuff ?

For instance:

helloworld.cpp

export module helloworld; // module declaration
 
import <iostream>;        // import declaration
 
export void hello()       // export declaration
{
    std::cout << "Hello world!\n";
}

main.cpp

import helloworld; // import declaration
 
int main()
{
    hello();
}

VERSUS

helloworld.h / helloworld.cpp

include <iostream>;

namespace ns
{
    void hello();
}


#include "helloworld.h";

void ns::hello()
{
    std::cout << "Hello world!\n";
}

main.cpp

#include "helloworld.h";
 
int main()
{
    ns::hello(); // optionally "using namespace ns; to avoid ns::"
}
Pierre Baret
  • 1,773
  • 2
  • 17
  • 35
  • 4
    Modules are an alternative to the traditional header+source file structure. When to use - never, until they gain widespread support in compilers. Then - always (except some cases where they can't be used, e.g. to import macros from a different file). – HolyBlackCat Jan 10 '23 at 07:34
  • 1
    What I learned from this new feature is that it accelerates the compilation. Here is a good reddit thread about it: https://www.reddit.com/r/cpp/comments/e2c1ca/c20_modules_and_build_performance/ – jdenozi Jan 10 '23 at 08:03
  • 1
    Currently there is no support for Makefile generation. You can get automated header file dependencies for automatic build of objects related to them. But there is nothing like this for modules in gcc for example. As this, it is more or less unusable in the moment or you want to write down all your dependencies by hand... nightmare! – Klaus Jan 10 '23 at 08:09
  • @Neofelis from what I understand it is mainly to avoid having to care about build order when mannually writting makefile – Pierre Baret Jan 10 '23 at 08:24
  • 1
    See also this question: https://stackoverflow.com/questions/70719627/should-i-be-using-c-modules-instead-of-include – nielsen Jan 10 '23 at 08:51
  • 1
    That modules is today only on the half of the way see here for auto dependencies with no answers :-) https://stackoverflow.com/questions/69190433/auto-generate-dependencies-for-modules – Klaus Jan 10 '23 at 08:55
  • After discussion with a fellow C++ dev, he told me that one of the ideas behind module is that we are seeing a lot of "header only libraries" now but those completly kill compilation time, that this highlights the fact that with template (that can't be implemented in .cpp) and other advanced c++ feature there are less and and less reason to keep 2 files (.h and .cpp), but in the end he really doubts it will be the way to go for most people until at least 5 years... – Pierre Baret Jan 10 '23 at 09:14
  • Stay away from modules for now. The community is having a hard time to implement it and right now I believe only MS supports it in full. Just wait for adoption to grow. – Something Something Jan 10 '23 at 10:34
  • @HenriqueBucher: "*Just wait for adoption to grow.*" How can adoption grow if people don't use them? – Nicol Bolas Jan 10 '23 at 14:26
  • @NicolBolas Great question. I dont think there is a good case for modules anyway. – Something Something Jan 10 '23 at 18:44
  • I get henrique's point about the current limited support for module anyway and I also get Nicol's point about the need for optimized compile time on some big code basis. My conclusion is that modules help for the latter and that gives me insights on whether and when to use it (or not necessarly) which was my question in the beginning ;-) – Pierre Baret Jan 10 '23 at 20:00

0 Answers0