为什么 std::regex_search 找不到合适的重载?
Why can't std::regex_search find the right overload?
我在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*>
相关文章:
- 继承函数的重载解析
- 你能重载对象变量名本身返回的内容吗
- 从父命名空间重载类型
- 使用C++中的模板和运算符重载执行矩阵运算
- 为什么这个运算符<重载函数对 STL 算法不可见?
- 重载操作程序时出错>>用于类中的字符串 memebr
- 一个关于在C++中重载布尔运算符的问题
- 不同翻译单元中不可重载的非内联函数定义
- 为什么使用SFINAE而不是函数重载
- 为什么我不能在 C++ 中的特定函数重载中调用同一函数的任何其他重载?
- 将重载的成员函数传递给函数模板
- c++:可变模板和函数重载
- 重载元组索引运算符-C++
- 如何使用重载的相等(==)运算符向测试用例添加描述
- 重载==不适用于二进制树
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 重载运算符new[]的行为取决于析构函数
- 正在尝试重载二进制搜索树分配运算符
- 重载Singly Linked List中的赋值运算符
- 没有与参数列表匹配的重载函数"search"实例