( C++20)
, . .
// helloworld.cpp
export module helloworld; //
import <iostream>; //
export void hello() { //
std::cout << " !\n";
}
// main.cpp
import helloworld; //
int main() {
hello();
}
export() module - - () () ;
|
(1) | ||||||||
export
|
(2) | ||||||||
export { - () }
|
(3) | ||||||||
export() import - () ;
|
(4) | ||||||||
export() import - () ;
|
(5) | ||||||||
export() import - () ;
|
(6) | ||||||||
module ;
|
(7) | ||||||||
module : private ;
|
(8) | ||||||||
, . , , ( , ). (, , ), .
export() module - - () () ;
|
|||||||||
, (: mymodule, mymodule.mysubmodule, mymodule2...). , .
.
, export, ; .
, ; . .
// ( )
export module A; //
// 'A'
module A; // 'A'
module A; //
// 'A'
export module A.B; //
// 'A.B'
module A.B; // 'A.B'
( ), . , export export.
export
|
|||||||||
export { - () }
|
|||||||||
export module A; //
// 'A'
// hello() , 'A'
export char const* hello() { return ""; }
// world() .
char const* world() { return ""; }
// one(), zero().
export {
int one() { return 1; }
int zero() { return 0; }
}
// : hi::english() hi::french().
export namespace hi {
char const* english() { return "!"; }
char const* french() { return "!"; }
}
:
export() import - () ;
|
|||||||||
, , .
. , A - B, A B.
( -) .
/////// A.cpp ( 'A')
export module A;
export char const* hello() { return ""; }
/////// B.cpp ( 'B')
export module B;
export import A;
export char const* world() { return ""; }
/////// main.cpp ( )
#include <iostream>
import B;
int main() {
std::cout << hello() << ' ' << world() << '\n';
}
#include ( ), . :
export() import - () ;
|
|||||||||
, . . ( ). , #include, , , . ( ), .
. ( ). , #include, , , . ( ), .
/////// A.cpp ( 'A')
export module A;
import <iostream>;
export import <string_view>;
export void print(std::string_view message) {
std::cout << message << std::endl;
}
/////// main.cpp ( )
import A;
int main() {
std::string_view message = ", !";
print(message);
}
, , (, ).
module;
-- () - |
|||||||||
/////// A.cpp ( 'A')
module;
// _POSIX_C_SOURCE
// POSIX.
#define _POSIX_C_SOURCE 200809L
#include <stdlib.h>
export module A;
import <ctime>;
// ( ).
// C++ <random>.
export double weak_random() {
std::timespec ts;
std::timespec_get(&ts, TIME_UTC); // <ctime>
// <stdlib.h> POSIX.
srand48(ts.tv_nsec);
// drand48() 0 1.
return drand48();
}
/////// main.cpp ( )
import <iostream>;
import A;
int main() {
std::cout << " 0 1: " << weak_random() << '\n';
}
, , .
module : private ;
- () |
|||||||||
, . , .
export module foo;
export int f();
module :private; // ,
//
//
int f() { // foo
return 42;
}
. , , : .
export module A:B; // 'A', ':B'.
( ). ( ).
.
export() import - () ;
|
|||||||||
/////// A-B.cpp
export module A:B;
...
/////// A-C.cpp
module A:C;
...
/////// A.cpp
export module A;
import :C;
export import :B;
...
, , .
( export). - , .
export() import - () ;
|
|||||||||
/////// A.cpp
export module A; //
export import :B; // Hello() 'A'.
import :C; // WorldImpl() 'A.cpp'.
// export import :C; // : .
// World() , 'A'.
export char const* World() {
return WorldImpl();
}
/////// A-B.cpp
export module A:B; //
// Hello() , 'A'.
export char const* Hello() { return ""; }
/////// A-C.cpp
module A:C; //
// WorldImpl() 'A', ':C'.
char const* WorldImpl() { return ""; }
/////// main.cpp
import A;
import <iostream>;
int main() {
std::cout << Hello() << ' ' << World() << '\n';
// WorldImpl(); // : WorldImpl() .
}
, , .
, . .
export module lib_A;
int f() { return 0; } // f
export int x = f(); // x 0
export module lib_B;
int f() { return 1; } // OK, f lib_A f lib_B
export int y = f(); // y 1
, , ; , . :
- , .
- , .
export module lib_A;
export constexpr int f() { return 0; } // f
export module lib_B;
export constexpr int f() { return 1; }
// : f;
// : OK, f lib_A f lib_B
- (, , ):
- namespace ;
- .
export module lib_A;
namespace ns { // ns lib_A.
export extern "C++" int f(); // f lib_A.
extern "C++" int g(); // g lib_A.
export int h(); // h lib_A.
}
// ns::h lib_A, ns::f ns::g
// (, ).
__cpp_modules |
201907L |
(C++20) | — |
__cpp_lib_modules |
202207L |
(C++23) | std std.compat
|