3

Create a new empty C++ Windows console application in Visual Studio 2022.

Add a "main.cppm" file and populate it with this:

import <sstream>;

int main()
{
    std::stringstream ss;
    ss << "Hey world!";
    return 0;
}

It should compile and run fine (though it doesn't do anything useful).

Now comment that out and change the code to this:

//import <sstream>;
import <iostream>;

int main()
{
    std::cout << "Hello world!" << std::endl;
    //std::stringstream ss;
    //ss << "Hey world!";
    return 0;
}

Again, it compiles and runs fine and we see "Hello world!" on the console.

But now uncomment the commented lines:

import <sstream>;
import <iostream>;

int main()
{
    std::cout << "Hello world!" << std::endl;
    std::stringstream ss;
    ss << "Hey world!";
    return 0;
}

This will NOT compile, giving the following errors:

E0070 incomplete type is not allowed

(referring to stringstream)

E0349 no operator "<<" matches these operands

E3365 incomplete class type "std::basic_stringstream<char, std::char_traits<char>, std::allocator<char>>" is not allowed

and the following message:

Local variable is not initialized

(referring to the ss variable)

It compiles fine if I:

  1. #include <sstream> and import <iostream>;, or
  2. #include <iostream> and import <sstream>;, or
  3. #include <iostream> and #include <sstream>;.

But I CANNOT import <iostream>; and import <sstream>; at the same time.

Can anyone explain why, or know how to import both? I suspect this has to do with a dependency between the two, but I'm not sure.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Todd Burch
  • 200
  • 8
  • 4
    Exxxx diagnostics are not compile errors, merely spew from the Intellisense parser. This is very new, so liable to have trouble, file bug reports with Help > Send Feedback. – Hans Passant Apr 26 '23 at 02:42
  • Thanks Hans. I am fairly new to C++ as it is and wasn’t sure if I was doing something wrong. – Todd Burch Apr 26 '23 at 02:52
  • 2
    Why `import` and just just `#include` headers to begin with? – David C. Rankin Apr 26 '23 at 04:54
  • @DavidC.Rankin [Overview of modules in C++](https://learn.microsoft.com/en-us/cpp/cpp/modules-cpp?view=msvc-170) – Remy Lebeau Apr 26 '23 at 06:10
  • @ToddBurch most standard library units are available via `import std.core;` – Remy Lebeau Apr 26 '23 at 06:11
  • @RemyLebeau - thank you. A MS compiler thing. I've never run into a problem using `#include` with `cl.exe` or `gcc` or any other compiler, so I was a bit curious why the choice of one over the other. The independently compiled set of sources sounds interesting -- though I'm a little fuzzy on the problems with header files it is supposed to cure. Explains why `g++` puked on the source right off the bat `:)` – David C. Rankin Apr 26 '23 at 06:17
  • 1
    @DavidC.Rankin when your projects start getting large, compile times get prohibitively slow when using headers (as well as being very inefficient). C++20 modules are designed to solve this (as well as a few other issues). – Todd Burch Apr 26 '23 at 06:47
  • @RemyLebeau I haven’t had success getting std.core to work yet, plus I think importing only the modules in use better documents the needs of the code—but I may end up heading that direction. – Todd Burch Apr 26 '23 at 06:49
  • 1
    I would avoid modules for another year or two, especially if you're new to C++. They are not widely/properly supported yet. If you have issues with build times, make sure you're using PCH. – HolyBlackCat Apr 26 '23 at 07:19
  • It does tend to make one speculate what the origin of modules was when the linked page explains "Before it was specified by the C++20 standard, Microsoft had experimental support for modules in the Microsoft C++ compiler. " Though the page cautions that for existing larger existing projects to "Base your adoption on whether you get a meaningful reduction in compilation times." Will be nice to experiment with and see what can be obtained. – David C. Rankin Apr 26 '23 at 11:41
  • 2
    @ToddBurch: Importing standard library components as header units is *supposed* to work, but it doesn't really work well on most implementations. – Nicol Bolas Apr 26 '23 at 13:49

2 Answers2

1

I tested with MSVC 2022 Version 17.5.4 and 17.6.0 Preview 5.0.

Edit: IDE says that C++ IntelliSense support for C++20 Modules is currently experimental.

The code compiles and runs.

Scan Sources for Module Dependencies needs to be Yes.

You can change this option as follows:

Menu -> Project -> Properties -> Configuration Properties -> C/C++ -> All Options -> Scan Sources for Module Dependencies -> Yes.

Scan Sources for Module Dependencies

pascal754
  • 189
  • 6
0

This is clearly a bug in Visual Studio, specifically IntelliSense. Note that module support is still incomplete there so until it is fixed you'll have to use includes. In C++23 you'll also be able to do import std.

vitaut
  • 49,672
  • 25
  • 199
  • 336
  • No, it isn't, not with the information provided. Diagnostics starting wtih E aren't from MSVC (those would be starting with C), but from IntelliSense, which is an IDE feature and not the C++ compiler. – Fulgen May 01 '23 at 19:22
  • 1
    @Fulgen Please note that the *old* acronym MSVC is commonly (though maybe imprecisely) used to referer to the *whole* Visual Studio IDE (which includes IntelliSense), not just the `cl.exe` compiler, if that is your concern about this answer. – Bob__ May 01 '23 at 21:26
  • @Bob__ Hmm, I'm also under impression that it refers to a compiler. For example, see [this](https://en.wikipedia.org/wiki/Microsoft_Visual_C%2B%2B), or [this](https://code.visualstudio.com/docs/cpp/config-msvc). – HolyBlackCat May 02 '23 at 06:46
  • Clarified that MSVC refers to VS, not the compiler. – vitaut May 02 '23 at 14:11
  • MSVC generally refers to the Microsoft Visual C++ toolset: the source compiler. See, for example, [this page that uses the acronym](https://learn.microsoft.com/en-us/cpp/windows/latest-supported-vc-redist?view=msvc-170). It's kind of confusing that Visual Studio has multiple C++ compilers: the IDE (e.g., IntelliSense) uses a different C++ compiler. Using "MSVC" to refer to a bug that manifests specifically in the IDE is confusing. – apardoe May 02 '23 at 14:41