Pie*_*ret 5 c++ c++20 c++-modules
这个问题可能太宽泛了,但最近确实让我很高兴:
我最近发现了现代 C++ 中的模块: https://en.cppreference.com/w/cpp/language/modules
但我真的不明白他们的目的以及何时使用它而不是命名空间或只是库头?
在提供的示例中,他们现在使用import <iostream>;
代替include <iostream>;
,使用其中一种与另一种有什么区别?哪一个是首选?
他们说“模块与名称空间正交”?这是什么意思?
关于开发的指导方针是什么,我们现在应该避免标题和东西吗?
例如:
你好世界.cpp
export module helloworld; // module declaration
import <iostream>; // import declaration
export void hello() // export declaration
{
std::cout << "Hello world!\n";
}
Run Code Online (Sandbox Code Playgroud)
主程序
import helloworld; // import declaration
int main()
{
hello();
}
Run Code Online (Sandbox Code Playgroud)
相对
helloworld.h / helloworld.cpp
include <iostream>;
namespace ns
{
void hello();
}
#include "helloworld.h";
void ns::hello()
{
std::cout << "Hello world!\n";
}
Run Code Online (Sandbox Code Playgroud)
主程序
#include "helloworld.h";
int main()
{
ns::hello(); // optionally "using namespace ns; to avoid ns::"
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1370 次 |
最近记录: |