VS2022刚刚升级到了17.2.0的版本,宣称是已完全支持C++20。按默认的安装方式,一般是不会安装标准库的模块文件的,需要打开Visual Studio Installer,安装单独的“C++ 模块”组件(在单个组件搜索时一定得加那个空格)。模块安装完成后,按微软的官方文档,将项目属性->C/C++->语言页内:C++语言标准设置成:/std:c++20,启用实验性的C++标准库模块:是(/experimental:module)。
import std.core;
int main()
{
const size_t max{ 100 }; // Number of primes required
long primes[max]{ 2L }; // First prime defined
size_t count{ 1 }; // Count of primes found so far
long trial{ 3L }; // Candidate prime
while (count < max)
{
bool isPrime{ true }; // Indicates when a prime is found
// Try dividing the candidate by all the primes we have
for (size_t i{}; i < count && isPrime; ++i)
{
isPrime = trial % *(primes + i) > 0; // False for exact division
}
if (isPrime)
{
// We got one...
*(primes + count++) = trial; // ...so save it in primes array
}
trial += 2; // Next value for checking
}
// Output primes 10 to a line
std::cout << "The first " << max << " primes are:\n";
for (size_t i{}; i < max; ++i)
{
std::cout << std::format("{:7}", *(primes + i));
if ((i + 1) % 10 == 0) // Newline after every 10th prime
std::cout << '\n';
}
std::cout << std::endl;
}
此时可以正常编译,但生成时会有三四个兼容性问题的警告。
1>C:\Users\水龙月\source\repos\Beginning C++20\Chapter 06\Example_05\Main.cpp(1,16): warning C5050: 导入模块“std.core”时可能不兼容的环境: _GUARDOVERFLOW_CRT_ALLOCATORS=1 is defined in current command line and not in module command line
1>C:\Users\水龙月\source\repos\Beginning C++20\Chapter 06\Example_05\Main.cpp(1,16): warning C5050: 导入模块“std.core”时可能不兼容的环境: _DEBUG is defined in current command line and not in module command line
1>C:\Users\水龙月\source\repos\Beginning C++20\Chapter 06\Example_05\Main.cpp(1,16): warning C5050: 导入模块“std.core”时可能不兼容的环境: _M_FP_PRECISE is defined in current command line and not in module command line
1>C:\Users\水龙月\source\repos\Beginning C++20\Chapter 06\Example_05\Main.cpp(1,16): warning C5050: 导入模块“std.core”时可能不兼容的环境: mismatched C++ versions. Current "202002" module version "202004"
消除警告:
项目属性->C/C++->所有选项:
1、C++语言标准:最新草案(/std:c++latest),消除最后一条警告。
2、SDL检查:否(/sdl-),消除第一条警告。
3、浮点模型:未设置(删除里面的值),消除第三条警告。
4、预处理器定义:将_DEBUG删除,消除第二条警告。
(实际项目在其他盘里,C盘用户目录里放了指向其文件夹的软链接。)
|