[try Beta version]
Not logged in

 
C++20 import issue

Sep 13, 2023 at 9:33am
i tried to code in C++20 and tried to import header file but it doesn't worked

anyone knows why?

IDE i'm using is Visual Studio 2022

my code is like this:

1
2
3
4
5
6
7
8
9
import <iostream>;

int main () {
	std::cout << "iostream imported" << std::endl;

	//...

	return 0;
}
Sep 13, 2023 at 12:14pm
Is your project settings set to use c++ 20?

Go to Project > Your Project Name > General > C++ Language Standard

You may need to use Latest instead of C++ 20, but try 20 first

Then under C/C++ > language > Build ISO C++ 23 Standard Library Modules

Also enable Experimental C++ library Modules

Set both to to yes

Also right click your .cpp file in the solution explorer and go to Properties > advanced > Compile as > Compile as C++ Module Code (/interface )

Does that work?
Last edited on Sep 13, 2023 at 12:39pm
Sep 13, 2023 at 3:28pm
use import std; which imports all of std::

1
2
3
4
5
import std;

int main() {
	std::cout << "Hello world\n";
}

Last edited on Sep 13, 2023 at 3:28pm
Sep 13, 2023 at 10:34pm
Sep 14, 2023 at 12:04am
In my experience with Visual Studio 2022 and C++20 modules there is no need to enable the Experimental C++ Standard Library Modules or enabling ISO C++23 Standard Library Modules if you import individual modules that are also C++ headers such as <iostream>.

Your language standard must be at least std:c++20 To use that form of importing stdlib headers/modules.

If you use import std; the language standard must be std:c++latest. No need to enable the experimental or C++23 modules that I've run across.

Intellisense sometimes has issues when using modules, though the code compiles without a problem. Especially if you use import std;.

It is possible to change defaults like the C++ language standard globally in the VS IDE so every new project/solution uses std:c++20 or std::c++latest instead of the default C++14 language standard. You can also change/update other properties to be default such as the experimental C++ and C++23 stdlib modules setting. Doing it requires a bit of work, but it is worth the effort IMO.

Visual Studio - Connecting a Library
https://cplusplus.com/forum/lounge/271176/#msg1169093

I did this so every new project uses C++20 as the default language standard. I set this up before C++23 was an option in the property pages, and never had a need for the experimental C++ stdlib modules. I will probably change those default properties in the near future.
Sep 14, 2023 at 2:57pm
@ch1156: thanks!! it's working!!


You may need to use Latest instead of C++ 20, but try 20 first


but there's no "Latest" option

https://imgur.com/a/VzxIaZY

nevertheless, it is already working... thanks, bro!

@george: ok, thanks fyi

so, you're saying that i just have to set both of those to "No"?
Last edited on Sep 14, 2023 at 3:22pm
Sep 14, 2023 at 4:12pm
but there's no "Latest" option


That's the preview option....
Sep 14, 2023 at 4:54pm
but there's no "Latest" option

Look at the option below C++20, the one that is marked "Preview".

Far right shows "std:c++latest".

so, you're saying that i just have to set both of those to "No"?

You don't have to do anything, the defaults are already set so you don't use the experimental C++20 standard library modules, the option is blank, and the C++23 standard library modules option is set to No.

If you choose to use C++23 import modules, import std; for example, then you have to set the C++ language standard to Preview/std:c++latest.

Personally I prefer to specify what modules my code consumes by providing the module name. import <iostream>; for example.

Doing that makes modularizing old pre-C++20 code easier since the change is changing #include to import and add the semicolon at the end.

Since VS is not 100% C++23 compliant yet at this time I stay with the C++20 language standard. I'm still getting to know what C++20 added to the language toolbox, learning what C++23 offers is "in the future."

Intellisense in the VS IDE even with C++20 goes kinda loopy. With C++23 it goes really bonkers, flagging code as errors that aren't. Code that compiles without any issues.
Topic archived. No new replies allowed.