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

标签 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));
    }

最佳答案

第二个重载需要一个

std::match_results<const CharT*,Alloc>

为第二个参数。这不匹配 mat具有类型

std::match_results<char>

将其更改为

std::match_results<const char*>

应该可以

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

关于c++ - 为什么 std::regex_search 找不到正确的重载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49370036/

相关文章:

c# - 如何调用 XML api

c++ - c++ 中是否有任何众所周知的基于文件的键-> 值数据结构可用?

c++ - 将参数转发给工作队列函数时出现编译错误

javascript - 如何更改现有的正则表达式以接受逗号

c# - foreach 循环在这里到底在做什么......帮助我准确地理解它

c++ - Windows 应用程序占用过多内存。有什么建议吗?

c++ - 为什么只检查某些值的错误? (C++?)

javascript - 为什么连续前瞻并不总是有效

python - re.sub 替换为匹配的内容

visual-studio - 如何将 Visual Studio SCC 插件与我的 CMAKE 生成的解决方案/项目集成