educative.io

The module import code sample seems wrong

The module usage sample code seems wrong, as it does not have any kind of import or pretty much nothing to do with modules. This is the code as I see it in the lesson:

#include
#include
using namespace std;

template
T add(T a, T b) {
return a + b;
}

int main() {
double f = add(1.23, 4.56);
int i = add(7, 42);
string s = add("one ", “two”);
cout <<
"double: " << f << “\n” <<
"int: " << i << “\n” <<
"string: " << s << “\n”;
return 0;
}


Course: C++20 STL Cookbook - Learn Interactively
Lesson: Avoid Re-Compiling Template Libraries with Modules

1 Like

Hello Marius,

It seems like the code you have added is missing:

#include <iostream>
#include <string>

This appears on the code at our end. Press the Reset button on the bottom right to ensure the default code appears.

The screenshot attached shows the intended code and the resulting output.

Please reach out if you still face any issues.

Right, that looks like it was a copy/paste error, I can see and included to. But that’s not what I’m referring to.

My question is what does that code have to do with modules, introduced in C++20? There should be some import directives to show the usage of modules. Otherwise, that sample code is nothing that wouldn’t work on C++11 or maybe even older.

Hello Marius,

Thank you for reaching out to us. The module support is available in Microsoft Visual C++ (MVCS), but it is not fully available with GCC 12,which is used on our platform and therefore, the codes with modules do not fully compile on our platform. Therefore, we provided the code using modules at the bottom of the page so that if the learner wants to learn module functionality on their own MVCS compiler. As of Visual Studio 2022 version 17.1, C++20 standard modules are fully implemented in the Microsoft C++ compiler.

Thank you for your response, and keep learning.

1 Like