c++17 `filesystem` 不是命名空间名称

2022-01-23 00:00:00 g++ c++ c++17 std

我想知道为什么在下面的代码中,没有找到命名空间filesystem:

<块引用>

g++ -std=c++17 main.cpp -lstdc++

//#include <文件系统><- 错误,所以改成如下:#include <实验性/文件系统>命名空间 fs = std::filesystem;主函数(){返回0;}

错误:

main.cpp:3:21: error: ‘filesystem’ is not a namespace-name命名空间 fs = std::filesystem;^main.cpp:3:31:错误:;"标记之前的预期命名空间名称命名空间 fs = std::filesystem;

<块引用>

gcc 版本 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.5)

解决方案

GCC 5.4.0于2016年6月发布;在采用 C++17 标准之前一年多.它及其版本的 libstdc++ 对 C++17 的支持非常有限.您可以在此处查看 GCC 何时添加 C++17 语言功能以及何时添加 libstdc++C++17 标准库功能这里.

在 GCC 5.4 发布时,文件系统库尚未在 std::filesystem 命名空间中实现.它与任何其他 <experimental/...>该版本中包含的标头位于 std::experimental 命名空间中.

I am wondering why in the following code, the namespace filesystem is not found:

g++ -std=c++17 main.cpp -lstdc++

// #include <filesystem>   <- error, so changed to the following:
#include <experimental/filesystem>

namespace fs = std::filesystem;

int main()
{
    return 0;
}

error:

main.cpp:3:21: error: ‘filesystem’ is not a namespace-name
 namespace fs = std::filesystem;
                     ^
main.cpp:3:31: error: expected namespace-name before ‘;’ token
 namespace fs = std::filesystem;

gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.5)

解决方案

GCC 5.4.0 was released in June of 2016; over a year before the C++17 standard was adopted. It and its version of libstdc++ have very limited C++17 support. You can see when GCC added C++17 language features here and when libstdc++ added C++17 standard library features here.

At the time of GCC 5.4's release, the filesystem library was not yet implemented in the std::filesystem namespace. It, along with any other <experimental/...> headers that are included in that version, are in the std::experimental namespace.

相关文章