Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't import both sstream and iostream in C++20?

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.

like image 356
Todd Burch Avatar asked Dec 09 '25 08:12

Todd Burch


1 Answers

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

like image 91
pascal754 Avatar answered Dec 11 '25 20:12

pascal754



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!