我想使用功能测试宏检查 std :::::::::::::::::: FileSystem
可用,但是即使我知道 std :: filesystem
, __ cpp_lib_filesystem
也无法定义。例如,以下测试程序:
#include <iostream>
int main () {
std::cout << "__cpp_lib_filesystem: "
#ifdef __cpp_lib_filesystem
<< __cpp_lib_filesystem
#else
<< "not defined"
#endif
<< std::endl;
std::cout << "__has_include(filesystem): "
#ifndef __has_include
<< "don't have __has_include"
#elif __has_include(<filesystem>)
<< "yes"
#else
<< "no"
#endif
<< std::endl;
}
// also, this compiles:
#include <filesystem>
std::filesystem::path test;
使用GCC 8.1(我的实际目标编译器)和GCC 11.2, -STD = C ++ 17
:
__cpp_lib_filesystem: not defined
__has_include(filesystem): yes
在这里,它在编译器资源管理器上。
我还尝试了包括&lt;版本&gt;
,但是,使用GCC 8.1,它不存在:
<source>:2:10: fatal error: version: No such file or directory
#include <version>
^~~~~~~~~
compilation terminated.
此外,这里的注释说:
库特征测试宏 - 在标题中定义&lt;版本&gt; (C ++ 20)
,除非我误解,否则这意味着库功能测试宏不在&lt; version&gt;
之前,直到C ++ 20(不适用于C ++) 17(尽管我不清楚这是否意味着 header 是C ++ 20功能,或者 macros 是否是C ++ 20功能)。
现在,在这种特殊情况下,我知道我可以通过这样做来测试它:
#if defined(__has_include) && __has_include(<filesystem>)
// ...
#else
// ...
#endif
在此处工作,因为在C ++ 17中正式添加了文件系统,并且 __ has_include
自C ++ 17(或者也许更早,我也不知道 - 即不应该在某种情况下 __ has_include
不可用,但是 std :: filesystem
is。所以很好。
但是,我的问题是关于 __ cpp_lib_filesystem
:为什么在上面的测试中不定义它?我想念什么 /如何使用它?
I wanted to use the feature test macros to check if std::filesystem
was available, but __cpp_lib_filesystem
isn't defined even when I know std::filesystem
is present. For example, the following test program:
#include <iostream>
int main () {
std::cout << "__cpp_lib_filesystem: "
#ifdef __cpp_lib_filesystem
<< __cpp_lib_filesystem
#else
<< "not defined"
#endif
<< std::endl;
std::cout << "__has_include(filesystem): "
#ifndef __has_include
<< "don't have __has_include"
#elif __has_include(<filesystem>)
<< "yes"
#else
<< "no"
#endif
<< std::endl;
}
// also, this compiles:
#include <filesystem>
std::filesystem::path test;
Outputs this with gcc 8.1 (my actual target compiler) and gcc 11.2, with --std=c++17
:
__cpp_lib_filesystem: not defined
__has_include(filesystem): yes
Here it is on Compiler Explorer.
I also tried including <version>
, but, with GCC 8.1, it's not present:
<source>:2:10: fatal error: version: No such file or directory
#include <version>
^~~~~~~~~
compilation terminated.
Additionally, the note here says:
Library feature-test macros - defined in the header <version> (C++20)
Which, unless I'm misinterpreting, means the library feature test macros aren't in <version>
until C++20, which doesn't apply to C++17 (although I'm not really clear if it means the header is the C++20 feature, or if the macros are the C++20 feature).
Now, in this particular case, I know I can test for it by doing:
#if defined(__has_include) && __has_include(<filesystem>)
// ...
#else
// ...
#endif
That'll work here because filesystem was officially added in C++17 and __has_include
has been around since C++17 (or maybe earlier, I dunno) as well -- i.e. there shouldn't be a situation where __has_include
isn't available but std::filesystem
is. So that's fine.
However, my question is about __cpp_lib_filesystem
: Why isn't it defined in the above test? What did I miss / how do I use it?
发布评论
评论(1)
使用
__ CPP_LIB_XXX
宏:实际上包括两种方法:
实际上包括相应的标头:
__ cpp_lib_constexpr_vector
即使包括&lt; vector&gt;
,也不会在C ++下定义。。
使用C ++ 20,并包括
&lt;版本&gt;
标题。There are two ways to use the
__cpp_lib_XXX
macros:Actually include the corresponding header: https://godbolt.org/z/xo68acnrz
__cpp_lib_constexpr_vector
will not be defined under C++17 even if<vector>
was included.Uses C++20, and include the
<version>
header.