0

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

错误 C2784:'bool std::regex_match(_BidIt,_BidIt,std::match_results<_BidIt,_Alloc> &,const std::basic_regex<_Elem,_RxTraits> &,std::regex_constants::match_flag_type)':无法推断'std::match_results>,_Alloc> &' 的模板参数来自 'std::cmatch'

见 '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]+"));
}
4

1 回答 1

2

问题是当迭代器类型是因为使用 a作为迭代std::regex_match器类型时无法解决函数重载。在这种情况下,两者都不合适,您需要自己的类型。这是应该如何完成的:gsl::continuous_span_iteratorstd::cmatchconst char*std::smatchstd::cmatchstd::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(".*"));
}

也就是说,在撰写本文时,由于问题 #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(".*"));
}

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

于 2016-02-24T23:08:50.753 回答