Git Product home page Git Product logo

cpp-module's Introduction

C++ 20 introduces modules, which allow compilers to use "semantic imports" instead of the old text inclusion model.

Modules allow shared declarations and definitions across translation units. A module is a set of source files that are compiled independently and only once. It has several advantages over header files:

  • Reduced compilation time.
  • Hide visibility of internal implementations like macros, preprocessor directives, and non-exported names.
  • Modules can be imported in any order without concern for dependencies between headers.

There is a simple example inside the examples folder, and a script containing the commands to run it.

Here is an article that compares header files, header units, modules and precompiled header. Support for modules of different compiler vendors can be found here (the Modules row in the 'C++20 core language features' table). This video gives a great introduction to modules.

Modules can be structured into several ways:

  • Primary Module Interface Unit

    Both module interface and implementation goes into a single file(like a header-only library).

    // File: my_module.cpp
    export module my_module;
    
    export char const* my_func() {
      return "Hello Modules!";
    }
  • Module Implementation Unit

    Module interface and implementation goes into a separate file(similar to header files and source files).

    // File: my_module.cpp
    export module my_module;
    
    export char const* my_func();
    
    // File: my_module_impl.cpp
    module my_module;
    
    char const* my_func() {
      return "Hello Modules!";
    }
  • Module Interface Partitions

    This is used for structuring the internal of a module

    // File: my_module.cpp
    export module m;
    
    export import :p1;
    export import :p2;
    
    // File: my_module_p1.cpp
    export module m:p1;
    export void p1_func() {}
    
    // File: my_module_p2.cpp
    export module m:p2;
    export void p2_func() {}

cpp-module's People

Contributors

jayyi3 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.