特性检测
以下是用于检测libstdc++
实现是否使用C预处理器定义的代码片段:
#include <regex>
#if __cplusplus >= 201103L && \
(!defined(__GLIBCXX__) || (__cplusplus >= 201402L) || \
(defined(_GLIBCXX_REGEX_DFS_QUANTIFIERS_LIMIT) || \
defined(_GLIBCXX_REGEX_STATE_LIMIT) || \
(defined(_GLIBCXX_RELEASE) && \
_GLIBCXX_RELEASE > 4)))
#define HAVE_WORKING_REGEX 1
#else
#define HAVE_WORKING_REGEX 0
#endif
宏
_GLIBCXX_REGEX_DFS_QUANTIFIERS_LIMIT
在 4.9.x
中在 bits/regex.tcc
中被定义
_GLIBCXX_REGEX_STATE_LIMIT
在 5+
中在 bits/regex_automatron.h
中被定义
_GLIBCXX_RELEASE
是 GCC 主要版本,在7+
中因此答案而添加
测试
您可以使用 GCC 进行如下测试:
cat << EOF | g++ --std=c++11 -x c++ - && ./a.out
#include <regex>
#if __cplusplus >= 201103L && \
(!defined(__GLIBCXX__) || (__cplusplus >= 201402L) || \
(defined(_GLIBCXX_REGEX_DFS_QUANTIFIERS_LIMIT) || \
defined(_GLIBCXX_REGEX_STATE_LIMIT) || \
(defined(_GLIBCXX_RELEASE) && \
_GLIBCXX_RELEASE > 4)))
#define HAVE_WORKING_REGEX 1
#else
#define HAVE_WORKING_REGEX 0
#endif
#include <iostream>
int main() {
const std::regex regex(".*");
const std::string string = "This should match!";
const auto result = std::regex_search(string, regex);
#if HAVE_WORKING_REGEX
std::cerr << "<regex> works, look: " << std::boolalpha << result << std::endl;
#else
std::cerr << "<regex> doesn't work, look: " << std::boolalpha << result << std::endl;
#endif
return result ? EXIT_SUCCESS : EXIT_FAILURE;
}
EOF
结果
以下是各个编译器的一些结果:
$ gcc --version
gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-11)
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ ./a.out
<regex> doesn't work, look: false
$ gcc --version
gcc (GCC) 6.2.1 20160830
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ ./a.out
<regex> works, look: true
$ gcc --version
gcc (Debian 4.9.2-10) 4.9.2
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ ./a.out
<regex> works, look: true
$ gcc --version
gcc (Ubuntu 6.2.0-5ubuntu12) 6.2.0 20161005
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ ./a.out
<regex> works, look: true
$ gcc --version
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ ./a.out
<regex> works, look: true
$ gcc --version
gcc (GCC) 6.2.1 20160830
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ clang --version
clang version 3.9.0 (tags/RELEASE_390/final)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
$ ./a.out # compiled with 'clang -lstdc++'
<regex> works, look: true
龙藏于此
这是完全不被支持的,依赖于GCC开发人员放入bits/regex*
头文件中的私有宏的检测。这些内容可能随时更改或消失。希望它们不会在当前的4.9.x、5.x、6.x版本中被删除,但它们可能会在7.x版本中消失。
如果GCC开发人员在7.x版本中添加了#define _GLIBCXX_HAVE_WORKING_REGEX 1
(或类似的,暗示暗示)并保持不变,那么可以更新这个代码片段以包含它,后续的GCC版本将与上述代码片段一起工作。
据我所知,所有其他编译器都在__cplusplus >= 201103L
时具有可用的<regex>
,但你的情况可能有所不同。
显然,如果有人在stdc++-v3
头文件之外定义了_GLIBCXX_REGEX_DFS_QUANTIFIERS_LIMIT
或_GLIBCXX_REGEX_STATE_LIMIT
宏,这将完全破坏。
<regex>
支持不完整。我们能为您提供哪些帮助? - kennytmlibstdc++
中regex
状态的信息,请参见http://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.2011。 - ecatmur<regex>
没有任何改变(也就是说,它仍然没有被实现)。" - rubenvb