为什么std :: regex_search找不到正确的重载?

时间:2018-03-19 18:30:06

标签: c++ regex visual-studio

我在Visual Studio 2017中有一个简单的正则表达式问题。据我所知,这应该与#2 overload at cppreference.com for 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 studio\2017\professional\vc\tools\msvc\14.12.25827\include\regex(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 studio\2017\professional\vc\tools\msvc\14.12.25827\include\regex(1182): note: see declaration of 'std::iterator_traits<_BidIt>'
1>        with
1>        [
1>            _BidIt=char
1>        ]
1>f:\sandbox\regexerror\regexerror\regexerror.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 studio\2017\professional\vc\tools\msvc\14.12.25827\include\regex(1182): error C3646: 'difference_type': unknown override specifier
1>c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.12.25827\include\regex(1182): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.12.25827\include\regex(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 studio\2017\professional\vc\tools\msvc\14.12.25827\include\regex(1182): note: see declaration of 'std::iterator_traits<_BidIt>'
1>        with
1>        [
1>            _BidIt=char
1>        ]
1>c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.12.25827\include\regex(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 studio\2017\professional\vc\tools\msvc\14.12.25827\include\regex(1182): note: see declaration of 'std::iterator_traits<_BidIt>'
1>        with
1>        [
1>            _BidIt=char
1>        ]
1>c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.12.25827\include\regex(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 studio\2017\professional\vc\tools\msvc\14.12.25827\include\regex(646): error C3646: 'value_type': unknown override specifier
1>c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.12.25827\include\regex(646): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.12.25827\include\regex(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 studio\2017\professional\vc\tools\msvc\14.12.25827\include\regex(1182): note: see declaration of 'std::iterator_traits<_BidIt>'
1>        with
1>        [
1>            _BidIt=char
1>        ]
1>c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.12.25827\include\regex(647): error C3646: 'difference_type': unknown override specifier
1>c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.12.25827\include\regex(647): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>f:\sandbox\regexerror\regexerror\regexerror.cpp(7): error C2672: 'std::regex_search': no matching overloaded function found
1>f:\sandbox\regexerror\regexerror\regexerror.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 studio\2017\professional\vc\tools\msvc\14.12.25827\include\regex(2948): note: see declaration of 'std::regex_search'
1>f:\sandbox\regexerror\regexerror\regexerror.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 studio\2017\professional\vc\tools\msvc\14.12.25827\include\regex(2936): note: see declaration of 'std::regex_search'
1>f:\sandbox\regexerror\regexerror\regexerror.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 studio\2017\professional\vc\tools\msvc\14.12.25827\include\regex(2919): note: see declaration of 'std::regex_search'
1>f:\sandbox\regexerror\regexerror\regexerror.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 studio\2017\professional\vc\tools\msvc\14.12.25827\include\regex(2904): note: see declaration of 'std::regex_search'
1>f:\sandbox\regexerror\regexerror\regexerror.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 studio\2017\professional\vc\tools\msvc\14.12.25827\include\regex(2890): note: see declaration of 'std::regex_search'
1>f:\sandbox\regexerror\regexerror\regexerror.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 studio\2017\professional\vc\tools\msvc\14.12.25827\include\regex(2877): note: see declaration of 'std::regex_search'
1>f:\sandbox\regexerror\regexerror\regexerror.cpp(7): note: could be 'std::match_results<char,std::allocator<std::sub_match<_BidIt>>>'
1>        with
1>        [
1>            _BidIt=char
1>        ]
1>f:\sandbox\regexerror\regexerror\regexerror.cpp(7): note: or       'const char *'
1>f:\sandbox\regexerror\regexerror\regexerror.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 studio\2017\professional\vc\tools\msvc\14.12.25827\include\regex(2877): note: see declaration of 'std::regex_search'
1>f:\sandbox\regexerror\regexerror\regexerror.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 studio\2017\professional\vc\tools\msvc\14.12.25827\include\regex(2864): note: see declaration of 'std::regex_search'
1>f:\sandbox\regexerror\regexerror\regexerror.cpp(7): note: could be 'std::match_results<char,std::allocator<std::sub_match<_BidIt>>>'
1>        with
1>        [
1>            _BidIt=char
1>        ]
1>f:\sandbox\regexerror\regexerror\regexerror.cpp(7): note: or       'const char *'
1>f:\sandbox\regexerror\regexerror\regexerror.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 studio\2017\professional\vc\tools\msvc\14.12.25827\include\regex(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));
    }

1 个答案:

答案 0 :(得分:4)

第二次重载需要

std::match_results<const CharT*,Alloc>

表示第二个参数。这与类型为

mat不匹配
std::match_results<char>

将其更改为

std::match_results<const char*>

应该工作

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