c++ - gsl::span 无法使用 std::regex 进行编译

标签 c++ regex c++11 guideline-support-library

我正在尝试使用 gsl::span 从混合二进制/ascii 数据的打包结构中传递一些数据(因此没有 vectorstring ) 到一个函数,我想用正则表达式对其进行操作,但出现以下错误:

error C2784: 'bool std::regex_match(_BidIt,_BidIt,std::match_results<_BidIt,_Alloc> &,const std::basic_regex<_Elem,_RxTraits> &,std::regex_constants::match_flag_type)' : could not deduce template argument for 'std::match_results>,_Alloc> &' from 'std::cmatch'

see declaration of 'std::regex_match'

这是我正在尝试做的事情:

#include <regex>
#include "gsl.h"

using namespace std;
using namespace gsl;

int main(int argc, const char **argv) 
{
    char lat[8] = { '0', '1', '9', '0', '0', '0', '0', 'E' };
    span<char> s = lat;

    // in a complex implementation this would be in a function,
    // hence the desire for span<>
    std::cmatch match;
    std::regex_match(s.begin(), s.end(), match, std::regex("[0-9]+"));
}

最佳答案

问题是 std::regex_match 在迭代器类型为 gsl::continuous_span_iterator 时无法解析函数重载,因为 std::cmatch 使用 const char* 作为迭代器类型。 std::smatchstd::cmatch 都不适合这种情况,您需要自己的 std::match_results 类型。以下是应该如何完成:

#include <regex>
#include "gsl.h"

using namespace std;
using namespace gsl;

int main(int argc, const char **argv) 
{
    char lat[8] = { '0', '1', '9', '0', '0', '0', '0', 'E' };
    span<char> s = lat;
    std::match_results<decltype(s)::iterator> match;
    std::regex_match(s.begin(), s.end(), match, std::regex(".*"));
}

也就是说,在撰写本文时,由于 issue #271,修改后的迭代器方法仍然无法编译。 .

在解决此问题之前,另一个解决方法是:

int main(int argc, const char **argv) 
{
    char lat[8] = { '0', '1', '9', '0', '0', '0', '0', 'E' };
    span<char> s = lat;
    std::cmatch match;
    std::regex_match(&s[0], &s[s.length_bytes()], match, std::regex(".*"));
}

解决方法涵盖将相同或不同范围的跨度传递给函数的情况。

关于c++ - gsl::span 无法使用 std::regex 进行编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35615098/

相关文章:

c++ - 带有 GCC 和 clang 的 constexpr char 数组

c++ - C++编程错误

c++ - 段错误(可能是由于转换)

java - 正则表达式代表组中的 "NOT"

Javascript 正则表达式 - 使用什么来验证电话号码?

c++ - 用 str(const char*) 设置 std::stringstream 的内容会产生奇怪的后果

c++ - 为英特尔 C++ Composer "GCC not found"设置环境变量时出现问题

c++ - 从 MinGW 链接到 OpenCV 的问题

python - 使用正则表达式从 html 文本中获取两个或多个连续大写单词

c++ - 插入通用容器不起作用