Mixing import and include
Can you mix #include directives and import statements in the same file?
Yes, you can mix #include
directives and import
statements in the same file. This flexibility is particularly useful when transitioning existing codebases to C++20 modules. However, there are some best practices and considerations to keep in mind.
Mixing #include
and import
When mixing #include
and import
, the order and placement of these directives matter. #include
directives are handled by the preprocessor, which performs a literal copy-paste of the included content.
import
statements, on the other hand, are part of the compiler's module system and provide better encapsulation and faster compile times.
Here's an example where we combine both techniques:
#include <iostream> // Traditional include
import Math; // Importing a module
int main() {
std::cout << "Result: " << add(2, 3);
}
Result: 5
Best Practices
Global Module Fragment: Use a global module fragment to handle #include
directives that must appear before the module
declaration. This is useful for including headers that define macros or other preprocessor directives.
module; // Global module fragment
// Legacy include
#include <SomeLegacyHeader.h>
export module MyModule;
import <iostream>;
export void myFunction() {
std::cout << "Using both include and import";
}
Avoid Mixing in Same Scope: Where possible, avoid mixing #include
and import
within the same scope. This can reduce potential conflicts and improve readability.
#include <vector> // Traditional include
import MyModule; // Importing a module
int main() {
// Use of both imported and included content
myFunction();
std::vector<int> vec{1, 2, 3};
for (int num : vec) {
std::cout << num << "\n";
}
}
Transition Gradually: When transitioning to modules, start by converting commonly used headers to header units or modules. Gradually replace #include
directives with import
statements to maintain compatibility.
Potential Issues
- Name Conflicts: Mixing
#include
andimport
can sometimes lead to name conflicts. Ensure that names used in different headers and modules do not clash. - Preprocessor Directives:
#include
directives introduce preprocessor macros, which are not managed by the module system. This can lead to unexpected behavior if not handled carefully.
Summary
Mixing #include
directives and import
statements in the same file is possible and useful during the transition to C++20 modules. By following best practices and understanding potential issues, you can effectively manage and modernize your codebase.
C++20 Modules
A detailed overview of C++20 modules - the modern alternative to #include
directives. We cover import
and export
statements, partitions, submodules, how to integrate modules with legacy code, and more.