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::"
}