c++visual-c++c++20c++-modules

In module, importing <string> after <istream> and <iostream> causes "incomplete class type"


I'm writing a module to do some setting under C++ 20 standard, with Visual Studio.

I found that some methods such as std::cin.get() and std::cin.clear() requires importing not only <iostream> but also <istream>.

However, when I import <string> after <iostream> and <istream>, the compiler suggests that in method std::cin.get() ,

incomplete class type "std::basic_istream<char, std::char_traits >" is not allowed

My code:

// ConsoleSetting.ixx

export module ConsoleSetting;

import <istream>;
import <iostream>;
import <string>;


int test() {
    char a;
    std::cin >> a;
    std::cin.get();  // incomplete class type "std::basic_istream<char, std::char_traits<char> >" is not allowed
}

I've tried importing <istream>, <iostream> and <string> in different orders, and it turns out that:


Solution

  • That's not the compiler's fault. It's the Intellisense, the corresponding static syntax checker, that cannot recognize it as correct. You can normally compile it (as I've copied your code and compiled it on my VS2019) and run the generated program, but your IDE prompts frustrating spurious red squiggles and an error list. As illustrated in Known issues with IntelliSense for C++20 modules, you may code like:

    #ifdef __INTELLISENSE__
    #include <iostream>
    #else
    import <iostream>;
    #endif
    

    to get the Intellisense to work.

    Now, the auto-completion of module code and Intellisense in Visual Studio is surprisingly unsatisfying. I recommend you not to use modules on formal occasions currently.

    FYI, the uniform way to import the whole standard library is import std; in C++23.