Use of import instead of #include

Hello all,

Is it now possible to use import instead of #include in C++ programs?

If yes, how to use import in this sample code?

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

using namespace std;

int main()
{
    cout << "Hello World!\n"; 

	system("pause");
	return 0;
}


If no, when will we be able to use import instead of #include in C++?
Last edited on
It looks like import will be possible in C++20.

1
2
3
4
5
6
7
8
9
10
11
12
import <iostream>;
import <cstdlib>;

using namespace std;

int main()
{
	cout << "Hello World!\n"; 
	
	system("pause");
	return 0;
}

Not sure any compiler supports it yet.
Thank you.
For that I got 13 errors:

#1 says:
Severity Code Description Project File Line Suppression State Error (active) E0864 import is not a template

#2 says: Severity Code Description Project File Line Suppression State Error (active) E0020 identifier "iostream" is undefined

...

I'm using MS VS 2019 version: 16.0.4

Is the compiler OK for that? Or should I apply some modifications on it or use another compiler, please?
Visual Studio 2019 doesn't support C++20 features (yet?). C++20 isn't standard yet, so at best you'd have experimental features, but I don't see modules supported.

Listed as not supported here: https://en.cppreference.com/w/cpp/compiler_support
No mention of import/modules in this article: https://devblogs.microsoft.com/cppblog/cpp17-20-features-and-fixes-in-vs-2019/

Edit: Found a first-party link. It's not supported.
https://docs.microsoft.com/en-us/cpp/overview/visual-cpp-language-conformance?view=vs-2019
Last edited on
MSVC 2019 does support modules, but I'm not sure if it's the exact syntax Peter87 used above. You have to pass /experimental:module to the compiler to enable them. Needless to say, experimental features are just meant to play around with, not for production code.
What is the conclusion, please?
Is it possible to use import in MS VS 2019? If so, using what code?

Hasn't any of you tested modules yet, please?
frek wrote:
I'm using MS VS 2019 version: 16.0.4

Is the compiler OK for that?

VS2017 has experimental module support, so does 2019

Using C++ Modules in Visual Studio 2017 | https://devblogs.microsoft.com/cppblog/cpp-modules-in-visual-studio-2017/

Example from the link:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// need to enable C+ Modules (experimental) and use C++17 or latest language standard
// example copied from
// https://blogs.msdn.microsoft.com/vcblog/2017/05/05/cpp-modules-in-visual-studio-2017/

#pragma warning(disable : 5050)  // to disable uncaught_exception warning

import std.core;

#pragma warning(disable : 4996)  // to disable uncaught_exception warning

int main()
{
   std::vector<std::string> v { "Plato", "Descartes", "Bacon" };
   std::copy(v.crbegin(), v.crend(), std::ostream_iterator<std::string>(std::cout, "\n"));
}


Modules support has to be manually switched on. In your project's property settings:

Configuration Settings -> C/C++ -> Language.

Change two switches:

1. C++ Language Standards - either std:c++17 or std:c++latest

2. Enable C++ Modules (experimental) - Yes

As others have said, use it at your own risk. Experimental means experimental. The standard hasn't been set yet.
I checked the settings using the link. And tested this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#pragma warning(disable : 5050)  // to disable uncaught_exception warning

import std.core;

#pragma warning(disable : 4996)  // to disable uncaught_exception warning
using namespace std;

int main()
{
	cout << "Hello import!, bye #include! :)" << endl;

	system("pause");
	return 0;
}


Still not working!
Last edited on
Do NOT use use namespace std; with modules. Did the linked example use it?

Don't use use namespace std; without modules for that matter.

1
2
3
4
5
6
7
8
9
10
#pragma warning(disable : 5050)  // to disable uncaught_exception warning

import std.core;

#pragma warning(disable : 4996)  // to disable uncaught_exception warning

int main()
{
   std::cout << "Hello import!, bye #include! :)" << std::endl;
}

Hello import!, bye #include! :)

Get used to typing the std:: qualifier, no matter what. It will make easily preventable headaches less likely to occur in the future.

system() is part of the C library, not C++. Modules don't pull in any of the C library at this experimental stage. Get away from all these "I'm still learning" tricks that are now coming back to bite you.
Thank you. I removed the stuff mentioned and re-used the code. But still 10 errors!
I guess the issue is one, small and subtle.

https://imgur.com/BbAcJ3v
Check your project's settings. I suspect they are not set properly.

In your project's property settings:

Configuration Settings -> C/C++ -> Language.

Change two switches:

1. C++ Language Standards - either std:c++17 or std:c++latest

2. Enable C++ Modules (experimental) - Yes

I've played around with import/modules since discovering the experimental support was available in VS 2017. And learned the hard way about the project switch settings. They have to be manually set with every new project. And best to check after making changes.

I'll be honest.....until the standard is set in C++20 for using modules the usage is minimal beyond playing around. IMO.

BTW, on further testing with system("pause"); I discovered a typo. It does work with modules pulling in the core std libraries, when fixed.

I personally am very allergic to using it. The function's system commands are not portable.
I'm very big on writing portable code. The effects of invoking a command depend on the system and library implementation, and may cause a program to behave in a non-standard manner or to terminate.

What works with Windows isn't likely to work with Linux/*nix or Mac.

Here is Configuration Settings -> C/C++ -> Language:
https://imgur.com/UHxv1rV

And this is Code Generation part:
https://imgur.com/Ba1Y11C

Now I get only two errors:

1- Severity Code Description Project File Line Suppression State Error (active) E2914 could not find module file "std.core" for import
2- Severity Code Description Project File Line Suppression State Error C1011 cannot locate standard module interface. Did you install the library part of the C++ modules feature in VS setup?

PS: I'm not sure if I've installed the library part of the C++ modules feature in VS setup! How to check it?
Last edited on
You can check and install the feature using Visual Studio Installler, as the error code suggests (VS setup).

Under Individual Components -> Compilers, build tools, and runtimes -> C++ Modules for v142 build tools (x64/x86 – experimental)

BTW, your picture of the project's language settings obscured the one setting that really needs to be seen: Enable C++ Modules (experimental). It is useless.
What's the point of this? What is or will be the difference between import and include?

Just curious
In theory, better compile times.
Topic archived. No new replies allowed.