2

I have watched a talk on cppcon here where the guy talks about modules. The talk was in 2021, so I figured such an important and long awaited language feature should be implemented by the 3 large compilers already (gcc, clang and msvc).

So I wrote a test program as such

import std;
auto main() -> int
{
    using std::cout;
    cout << "Hello world!\n";
    return 0;
}

However, no matter what I do, I can't make it compile. I tried import iostream;, import <iostream>;, import std.cout;, import std::cout;, etc.

I use the very latest version of the compilers available on my system (Ubuntu 22.04), which are gcc version 12 and clang version 17 respectively. I compile with -std=c++23 -Wall -Wextra -Wpedantic -O0 -g modules.cpp -o modules But g++ complains

import does not name a type

and clang gives me

module 'std' is not found

The error is of course on line 1. I tried using the example from the talk, in the hopes that somehow code written after the error would clear the error through some magic, but of course that didn't happen.

So what is going on here? How can that guy compile that code in 2021, but I can't compile that same code in 2023 with compilers released after 2021?

Why do we "have" module support, but they don't work? How can this guy tell thousands of people with a straight face that something works when it doesn't and hadn't for the next 2 years either?

Edit

I have seen this other question, which is why I included my example of import iostream;, which also didn't work, but was the accepted answer in that other question.

FalcoGer
  • 2,278
  • 1
  • 12
  • 34
  • fyi you are in implementation defined behaviour or UB with the definition of [`main`](https://en.cppreference.com/w/cpp/language/main_function) above – Richard Critten Jul 06 '23 at 10:34
  • @RichardCritten please elaborate – FalcoGer Jul 06 '23 at 10:35
  • 1
    See the _Standard Library Modules (P2465R3)_ row in the [C++23 conformance table](https://en.cppreference.com/w/cpp/compiler_support/23). – heap underrun Jul 06 '23 at 10:36
  • @FalcoGer _"It __shall__ have one of the following forms"_ The `main` above is neither (1) nor (2) and (3) is implementation defined. – Richard Critten Jul 06 '23 at 10:40
  • @RichardCritten I don't understand what you mean. int main() is perfectly valid. I just use the trailing return type syntax, which is also well defined. In fact clang-tidy was suggesting that I use it. If it was UB, I would think they'd add an exception for main. – FalcoGer Jul 06 '23 at 10:44
  • There is already an opinion-based question about this syntax for `main`: [Should main with trailing return type be avoided?](https://stackoverflow.com/q/24455223/1458097). – heap underrun Jul 06 '23 at 10:48
  • 2
    The OP says that the answer in the linked question does not work, so voting to reopen – Rohit Gupta Jul 06 '23 at 12:12
  • I can't spot any changes tried according to the duplicates answer in the compiler options as mentioned there. Still unclear what exactly doesn't work. I am leaving that closed atm. @rohit – πάντα ῥεῖ Jul 06 '23 at 12:36
  • Do you really want to import *all* of the `std` library? All my programs only include *what they need*. – Thomas Matthews Jul 06 '23 at 17:12
  • @ThomasMatthews but that's the beauty of it. no preprocessor means that you won't generate 5 million lines of code in your translation unit. compile time goes down, usability of the standard library goes up because you don't need to remember where stuff is anymore, and you skip the need of 20 lines of boilerplate to include all the headers and just do one line. the difference between including and importing a module is that include is a dumb copy and paste of text while importing is using the compiler, checking what you actually need by what you use. it's still linking against stdlib anyway – FalcoGer Jul 06 '23 at 20:06

0 Answers0