4

I don't understand why

  1. I saw import std.core; here
  2. I can't import std;
  3. I can't import std.iostream;
  4. I can #include <iostream>

Can you explain why above is happening? Maybe i guess std.iostream is not a module. Then why 1. works?

@Someprogrammerdue provided this reference, and its says

import <iostream>;         // import declaration

When I run following in my compiler

import <iostream>;

int main()
{

    return 0;
}

I get

main.cpp:1:8: error: 'iostream' was not declared in this scope
    1 | import<iostream>;

Why is this happening?

Inyoung Kim 김인영
  • 1,434
  • 1
  • 17
  • 38
  • Does this answer your question? [C++ include and import difference](https://stackoverflow.com/questions/172262/c-include-and-import-difference) – Rohan Bari Feb 26 '21 at 04:14
  • 1
    @RohanBari I understand that `#import` was a thing only for `MS`. But my question is about `import` without the `#` – Inyoung Kim 김인영 Feb 26 '21 at 04:18
  • `import` is new for [*modules*](https://en.cppreference.com/w/cpp/language/modules) which were added in the C++20 standard. If you search for e.g. `c++ modules` you might be able to find more information and tutorials. Of note, `import` is not the same (or even similar) to the preprocessor `#include` directive. – Some programmer dude Feb 26 '21 at 04:19

2 Answers2

9

I don't understand why

  1. I saw import std.core; here

You saw what you did because you read the page and that's what was written there.

  1. I can't import std;

This is because the C++20 standard library doesn't define modules. And because no other library can (or shouldn't) define module std because that module name is reserved for language implementation / future standardisation.

  1. I can't import std.iostream;

See 2.

  1. I can #include <iostream>

That header is part of the C++20 standard library.

Then why 1. works?

The MSVC documentation explains:

Although not specified by the C++20 standard, Microsoft enables its implementation of the C++ Standard Library to be imported as modules.

P.S. Support for modules is at the moment of writing only partially implemented by all major compilers with the exception of MSVC which appears to have full implementation since 19.28 (the modular standard libary is not a requirement for this).

P.P.S. Modular standard library is planned for C++23.

eerorika
  • 232,697
  • 12
  • 197
  • 326
0
  1. import std;

Improvement in compilation time.

According to ISO C++23, there is also import std.compat;.

Differences between the two are as follows (extracted from P2465R3):

import std; imports everything in namespace std from C++ headers (e.g. std::sort from <algorithm>) and C wrapper headers (e.g. std::fopen from <cstdio>). It also imports ::operator new etc. from <new>.

import std.compat; imports all of the above, plus the global namespace counterparts for the C wrapper headers (e.g. ::fopen).

import ... is said to be much faster to compile than #include ....

Interview with Bjarne Stroustrup (InfoWorld): C++ 23 to introduce module support.

You can check C++ compiler status here (https://en.cppreference.com/w/cpp/compiler_support). Search 'Standard Library Modules'(P2465R3) in 'C++23 library features' table.

P.S. Modules are not supported fully by most compilers yet. For MSVC users, please, refer to this link. (https://learn.microsoft.com/en-us/cpp/cpp/tutorial-import-stl-named-module?view=msvc-170).

pascal754
  • 189
  • 6