3

I'm starting to learn C++20, my first compilable language...

import <iostream>;

int main()
{
    int answer {42};
    std::cout << "The answer is "
            << answer
            << std::endl;
    return 0;

}

When I try to compile the file above, I get an error message due to the compiler not recognizing the statement import <iostream>;, even though I have the newest version of GCC compiler for Ubuntu 20.04.4 LTS.

康桓瑋
  • 33,481
  • 5
  • 40
  • 90
An old man in the sea.
  • 1,169
  • 1
  • 13
  • 30
  • 3
    `import` is a *really* new feature, and doesn't have wide compiler support yet. You don't want to be using it now, wait another year or two. For now, use the good ol' `#include`. If you got this code from what was advertised as a "beginner" C++ tutorial, you need to find a more reasonable tutorial. – HolyBlackCat May 01 '22 at 08:29
  • @TedLyngmo Cppreference suggests `import ;` [here](https://en.cppreference.com/w/cpp/language/modules#Importing_modules_and_headers), I think it's supposed to work. – HolyBlackCat May 01 '22 at 08:31
  • @HolyBlackCat Yes, I noticed that, which I think is a mistake. The standard says _"A module unit is a translation unit that contains a module-declaration. A named module is the collection of module units with the same module-name."_ and [iostream.sym](https://eel.is/c++draft/iostream.syn#header:%3ciostream%3e) shows no requirements on `iostream` to contain a module declaration. – Ted Lyngmo May 01 '22 at 08:35
  • @HolyBlackCat I think that's just explanatory code rather than anything standardised. Various implementations of modules for the standard library have been implemented but it hasn't yet been standardised – Alan Birtles May 01 '22 at 08:37
  • 1
    @TedLyngmo I found [`[headers]/4`](http://eel.is/c++draft/headers#4) which suggests the same syntax. – HolyBlackCat May 01 '22 at 08:37
  • @AlanBirtles See the link above. – HolyBlackCat May 01 '22 at 08:37
  • @HolyBlackCat Yes, that does indeed look like it is supposed to work. `` is even listed in that table as an _"importable"_. _"The headers listed in Table [23](http://eel.is/c++draft/headers#tab:headers.cpp), or, for a freestanding implementation, the subset of such headers that are provided by the implementation, are collectively known as the importable C++ library headers"_. Good find! – Ted Lyngmo May 01 '22 at 08:40
  • BTW, have you also added the command line option `-std=c++20`, when invoking the gcc compiler? Could you please add the exact error message to the question as text? – Bob__ May 01 '22 at 08:51
  • I can't find it now but I remember there was a similar question of a user trying to start learning C++ from a book that said to specifically teach C++20 and which started with importing standard library headers like that although it is not currently fully supported by compilers. If you are in a similar situation, I recommend a different book for now. – user17732522 May 01 '22 at 08:58
  • @user17732522 which book would you then recomend to start learning C++? – An old man in the sea. May 01 '22 at 09:08
  • https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – StoryTeller - Unslander Monica May 01 '22 at 09:09
  • 2
    @Anoldmaninthesea. If you are new to the language see the link in the comment above. Although there may be a few places that a beginner could make use of them, the features added in C++17 and C++20 are not necessarily that important at the beginning. C++14 had some relatively minor but useful improvements over C++11, but the most important thing is that the book teaches at least C++11 which changed _a lot_ from previous C++ revisions. – user17732522 May 01 '22 at 09:18
  • I'm curious, what book are you using now? – HolyBlackCat May 01 '22 at 09:20
  • @Bob__ how would I do that? Right now, I'm using vscode, and in the tasks.json file, the following command is run: `g++ -g -o `. You're suggesting to run: `g++ -g -o -std=c++20`? – An old man in the sea. May 01 '22 at 09:24
  • @HolyBlackCat I'm using `Beginning C++20: From Novice to Professional` by Norton and Weert. – An old man in the sea. May 01 '22 at 09:26
  • Yeah, pretty much, but see e.g. https://stackoverflow.com/questions/62765630/how-to-use-c20-modules-with-gcc – Bob__ May 01 '22 at 09:30
  • The author seems well aware of the issue that the code in his book isn't yet fully supported by compilers. I don't have it so I don't know whether he mentions that in the book or leaves the reader to figure it out for themselves, but he describes workarounds here: https://github.com/Apress/beginning-cpp20/tree/main/Workarounds – user17732522 May 01 '22 at 09:32
  • Potential duplicate (same book/code), although the user there tries it with MSVC instead of GCC: https://stackoverflow.com/questions/64877559/cant-use-iostream-as-module-in-c20-visual-studio – user17732522 May 01 '22 at 09:37
  • @Bob__ It seems that I need to use the `-std=c++2a` in GCC version that I have. When I run with that option, I get `error: ‘iostream’ was not declared in this scope`. – An old man in the sea. May 01 '22 at 09:48

1 Answers1

0

C++ 20 is considered a new standard (or maybe the latest standard). So most of the compilers do not have full coverage of everything in C++ 20 and the GCC compiler that you are using does not know what is import <iostream> because it does not have full coverage. What you could do, is to check out this list of compilers supporting C++ 20.