【问题标题】:gsl::span fails to compile with std::regexgsl::span 无法使用 std::regex 编译
【发布时间】:2016-02-24 23:08:50
【问题描述】:

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

错误 C2784:'bool std::regex_match(_BidIt,_BidIt,std::match_results<_bidit> &,const std::basic_regex<_elem> &,std::regex_constants::match_flag_type)':无法从 'std::cmatch' 推断出 'std::match_results>,_Alloc> &' 的模板参数

参见“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]+"));
}

【问题讨论】:

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


    【解决方案1】:

    问题是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(".*"));
    }
    

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

    【讨论】:

      猜你喜欢
      • 2021-05-13
      • 1970-01-01
      • 2021-04-29
      • 2017-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-12
      • 2015-06-04
      相关资源
      最近更新 更多