std::optional不编译
此代码不能使用命令 g++ -std=c++17 main.cpp 编译
#include <iostream>
#include <experimental/optional>
int main()
{
std::optional<int> x;
std::cout << "Hello World";
return 0;
}
错误如下:
- 错误:“可选”不是“标准”的成员
- 错误:'int' 之前的预期主表达式
有没有办法让这段代码编译?
回答
该<experimental/optional>头没有定义std::optional,而是std::experimental::optional。要获取std::optional,它是 C++17 标准的(非实验性)部分,您应该只需要#include <optional>.
试试神马。