C++20 Modules

Mixing import and include

Can you mix #include directives and import statements in the same file?

Abstract art representing computer programming

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 and import 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.

Answers to questions are automatically generated and may not have been reviewed.

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 124 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved