From Schmid.wiki
Jump to: navigation, search

Example Code

This is based on the current standard draft as of August 2016.

Let's create a module in mod.cpp:

module Mod; // Everything with the 'export' keyword in this file will be contained within the module 'Mod'

#include <iostream>
using namespace std;

export void hello() { cout << "hello" << endl; }

export class Thing {
public:
    void do_stuff() { cout << "doing stuff" << endl; }
};

We can then use 'Mod' in our main program, main.cpp:

import Mod;

int main() {
    Thing t;
    hello();
    t.do_stuff();
    return 0;
}

Visual C++ Example

This is an example of how to create and use a module in Visual Studio.

If we have the above files mod.cpp and main.cpp in a Visual Studio project (2015 update 1 or newer), we can enable the experimental module support.

To compile mod.cpp as a module, we need to add a specific compile flag for this file, so in the Solution Explorer, we can select Properties for mod.cpp and add the compile flag:

Configuration Properties:C/C++:Command Line:Additional Options:
 -module:export 

We need to add another compile flag for the project, so in the Properties for the project, we enable:

Configuration Properties:C/C++:Command Line:Additional Options:
 -experimental:module