C++

C++ 20 modules -short intro

I love the new modules feature of C++ 20, but , at least as far as G++ goes, it isn’t supported yet. While we wait for them to be an actual part, here is a small intro:

Cool thing number 1: we can use them to include files:

import <iostream>;
Code language: C++ (cpp)

This doesn’t seem like much, but now pch is included! Pretty awesome.

Cool thing number 2: Separate your work

//talreg.cppm export module talreg; namespace Talreg{ int add(int x, int y){return x+y;} export int mul(int x, int y){return x*y;} } // main.cpp import talreg; import<iostream> int main(){ std::cout<<"3*4="<<mul(3,4)<<std::endl; }
Code language: C++ (cpp)

You might want to notice the file extension – cppm.

Cool thing number 3: You don’t have to use header files

Admittedly, this might not be the right thing for huge modules, but it is still a great feature,

Leave a Reply

Your email address will not be published. Required fields are marked *