为什么 std::regex_search 找不到合适的重载?

Why can't std::regex_search find the right overload?

本文关键字:重载 search std regex 为什么 找不到      更新时间:2023-10-16

我在Visual Studio 2017中有一个简单的正则表达式问题。据我所知,这应该与 cppreference.com 的 #2 重载相匹配std::regex_search但编译器返回了大量错误。这是整个程序。

#include <regex>
int main()
{
   std::basic_regex<char> rx("This");
   std::match_results<char> mat;
   std::regex_search("This is a test.", mat, rx, std::regex_constants::match_continuous);
   return 0;
}

这是整个难以理解的错误列表。

1>------ Build started: Project: RegexError, Configuration: Debug Win32 ------
1>RegexError.cpp
1>c:program files (x86)microsoft visual studio2017professionalvctoolsmsvc14.12.25827includeregex(1182): error C2039: 'difference_type': is not a member of 'std::iterator_traits<_BidIt>'
1>        with
1>        [
1>            _BidIt=char
1>        ]
1>c:program files (x86)microsoft visual studio2017professionalvctoolsmsvc14.12.25827includeregex(1182): note: see declaration of 'std::iterator_traits<_BidIt>'
1>        with
1>        [
1>            _BidIt=char
1>        ]
1>f:sandboxregexerrorregexerrorregexerror.cpp(6): note: see reference to class template instantiation 'std::match_results<char,std::allocator<std::sub_match<_BidIt>>>' being compiled
1>        with
1>        [
1>            _BidIt=char
1>        ]
1>c:program files (x86)microsoft visual studio2017professionalvctoolsmsvc14.12.25827includeregex(1182): error C3646: 'difference_type': unknown override specifier
1>c:program files (x86)microsoft visual studio2017professionalvctoolsmsvc14.12.25827includeregex(1182): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:program files (x86)microsoft visual studio2017professionalvctoolsmsvc14.12.25827includeregex(1185): error C2039: 'value_type': is not a member of 'std::iterator_traits<_BidIt>'
1>        with
1>        [
1>            _BidIt=char
1>        ]
1>c:program files (x86)microsoft visual studio2017professionalvctoolsmsvc14.12.25827includeregex(1182): note: see declaration of 'std::iterator_traits<_BidIt>'
1>        with
1>        [
1>            _BidIt=char
1>        ]
1>c:program files (x86)microsoft visual studio2017professionalvctoolsmsvc14.12.25827includeregex(646): error C2039: 'value_type': is not a member of 'std::iterator_traits<_BidIt>'
1>        with
1>        [
1>            _BidIt=char
1>        ]
1>c:program files (x86)microsoft visual studio2017professionalvctoolsmsvc14.12.25827includeregex(1182): note: see declaration of 'std::iterator_traits<_BidIt>'
1>        with
1>        [
1>            _BidIt=char
1>        ]
1>c:program files (x86)microsoft visual studio2017professionalvctoolsmsvc14.12.25827includeregex(1422): note: see reference to class template instantiation 'std::sub_match<_BidIt>' being compiled
1>        with
1>        [
1>            _BidIt=char
1>        ]
1>c:program files (x86)microsoft visual studio2017professionalvctoolsmsvc14.12.25827includeregex(646): error C3646: 'value_type': unknown override specifier
1>c:program files (x86)microsoft visual studio2017professionalvctoolsmsvc14.12.25827includeregex(646): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:program files (x86)microsoft visual studio2017professionalvctoolsmsvc14.12.25827includeregex(647): error C2039: 'difference_type': is not a member of 'std::iterator_traits<_BidIt>'
1>        with
1>        [
1>            _BidIt=char
1>        ]
1>c:program files (x86)microsoft visual studio2017professionalvctoolsmsvc14.12.25827includeregex(1182): note: see declaration of 'std::iterator_traits<_BidIt>'
1>        with
1>        [
1>            _BidIt=char
1>        ]
1>c:program files (x86)microsoft visual studio2017professionalvctoolsmsvc14.12.25827includeregex(647): error C3646: 'difference_type': unknown override specifier
1>c:program files (x86)microsoft visual studio2017professionalvctoolsmsvc14.12.25827includeregex(647): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>f:sandboxregexerrorregexerrorregexerror.cpp(7): error C2672: 'std::regex_search': no matching overloaded function found
1>f:sandboxregexerrorregexerrorregexerror.cpp(7): error C2780: 'bool std::regex_search(const std::basic_string<_Elem,_StTraits,_StAlloc> &,const std::basic_regex<_Elem,_RxTraits> &,std::regex_constants::match_flag_type)': expects 3 arguments - 4 provided
1>c:program files (x86)microsoft visual studio2017professionalvctoolsmsvc14.12.25827includeregex(2948): note: see declaration of 'std::regex_search'
1>f:sandboxregexerrorregexerrorregexerror.cpp(7): error C2784: 'bool std::regex_search(const std::basic_string<_Elem,_StTraits,_StAlloc> &&,std::match_results<basic_string<_Elem,_StTraits,_StAlloc>::const_iterator,_Alloc> &,const std::basic_regex<_Elem,_RxTraits> &,std::regex_constants::match_flag_type)': could not deduce template argument for 'const std::basic_string<_Elem,_StTraits,_StAlloc> &&' from 'const char [16]'
1>c:program files (x86)microsoft visual studio2017professionalvctoolsmsvc14.12.25827includeregex(2936): note: see declaration of 'std::regex_search'
1>f:sandboxregexerrorregexerrorregexerror.cpp(7): error C2784: 'bool std::regex_search(const std::basic_string<_Elem,_StTraits,_StAlloc> &,std::match_results<basic_string<_Elem,_StTraits,_StAlloc>::const_iterator,_Alloc> &,const std::basic_regex<_Elem,_RxTraits> &,std::regex_constants::match_flag_type)': could not deduce template argument for 'const std::basic_string<_Elem,_StTraits,_StAlloc> &' from 'const char [16]'
1>c:program files (x86)microsoft visual studio2017professionalvctoolsmsvc14.12.25827includeregex(2919): note: see declaration of 'std::regex_search'
1>f:sandboxregexerrorregexerrorregexerror.cpp(7): error C2784: 'bool std::regex_search(const _Elem *,std::match_results<const _Elem*,_Alloc> &,const std::basic_regex<_Elem,_RxTraits> &,std::regex_constants::match_flag_type)': could not deduce template argument for 'std::match_results<const _Elem*,_Alloc> &' from 'std::match_results<char,std::allocator<std::sub_match<_BidIt>>>'
1>        with
1>        [
1>            _BidIt=char
1>        ]
1>c:program files (x86)microsoft visual studio2017professionalvctoolsmsvc14.12.25827includeregex(2904): note: see declaration of 'std::regex_search'
1>f:sandboxregexerrorregexerrorregexerror.cpp(7): error C2780: 'bool std::regex_search(const _Elem *,const std::basic_regex<_Elem,_RxTraits> &,std::regex_constants::match_flag_type)': expects 3 arguments - 4 provided
1>c:program files (x86)microsoft visual studio2017professionalvctoolsmsvc14.12.25827includeregex(2890): note: see declaration of 'std::regex_search'
1>f:sandboxregexerrorregexerrorregexerror.cpp(7): error C2782: 'bool std::regex_search(_BidIt,_BidIt,const std::basic_regex<_Elem,_RxTraits> &,std::regex_constants::match_flag_type)': template parameter '_BidIt' is ambiguous
1>c:program files (x86)microsoft visual studio2017professionalvctoolsmsvc14.12.25827includeregex(2877): note: see declaration of 'std::regex_search'
1>f:sandboxregexerrorregexerrorregexerror.cpp(7): note: could be 'std::match_results<char,std::allocator<std::sub_match<_BidIt>>>'
1>        with
1>        [
1>            _BidIt=char
1>        ]
1>f:sandboxregexerrorregexerrorregexerror.cpp(7): note: or       'const char *'
1>f:sandboxregexerrorregexerrorregexerror.cpp(7): error C2784: 'bool std::regex_search(_BidIt,_BidIt,const std::basic_regex<_Elem,_RxTraits> &,std::regex_constants::match_flag_type)': could not deduce template argument for '_BidIt' from 'std::match_results<char,std::allocator<std::sub_match<_BidIt>>>'
1>        with
1>        [
1>            _BidIt=char
1>        ]
1>c:program files (x86)microsoft visual studio2017professionalvctoolsmsvc14.12.25827includeregex(2877): note: see declaration of 'std::regex_search'
1>f:sandboxregexerrorregexerrorregexerror.cpp(7): error C2782: 'bool std::regex_search(_BidIt,_BidIt,std::match_results<_BidIt,_Alloc> &,const std::basic_regex<_Elem,_RxTraits> &,std::regex_constants::match_flag_type)': template parameter '_BidIt' is ambiguous
1>c:program files (x86)microsoft visual studio2017professionalvctoolsmsvc14.12.25827includeregex(2864): note: see declaration of 'std::regex_search'
1>f:sandboxregexerrorregexerrorregexerror.cpp(7): note: could be 'std::match_results<char,std::allocator<std::sub_match<_BidIt>>>'
1>        with
1>        [
1>            _BidIt=char
1>        ]
1>f:sandboxregexerrorregexerrorregexerror.cpp(7): note: or       'const char *'
1>f:sandboxregexerrorregexerrorregexerror.cpp(7): error C2784: 'bool std::regex_search(_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 '_BidIt' from 'std::match_results<char,std::allocator<std::sub_match<_BidIt>>>'
1>        with
1>        [
1>            _BidIt=char
1>        ]
1>c:program files (x86)microsoft visual studio2017professionalvctoolsmsvc14.12.25827includeregex(2864): note: see declaration of 'std::regex_search'
1>Done building project "RegexError.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

这是我认为应该匹配的<regex>的过载。

template<class _Elem,
    class _Alloc,
    class _RxTraits> inline
    bool regex_search(_In_z_ const _Elem *_Str,
        match_results<const _Elem *, _Alloc>& _Matches,
        const basic_regex<_Elem, _RxTraits>& _Re,
        regex_constants::match_flag_type _Flgs =
            regex_constants::match_default)
    {   // search for regular expression match in target text
    const _Elem *_Last = _Str + char_traits<_Elem>::length(_Str);
    return (_Regex_search1(_Str, _Last, &_Matches, _Re, _Flgs, _Str));
    }

第二个重载需要

std::match_results<const CharT*,Alloc>

对于第二个参数。 这与具有 mat 类型

不匹配
std::match_results<char>

将其更改为

std::match_results<const char*>

应该工作

此外,您可以改用std::cmatch,因为它被定义为std::match_results<const char*>