Sam*_*hik 5
这似乎是 libstdc++ 中的一个错误。使用我进入的调试器regex_replace
,直到进入这一部分:
// std [28.11.4] Function template regex_replace
/**
* @brief Search for a regular expression within a range for multiple times,
and replace the matched parts through filling a format string.
* @param __out [OUT] The output iterator.
* @param __first [IN] The start of the string to search.
* @param __last [IN] One-past-the-end of the string to search.
* @param __e [IN] The regular expression to search for.
* @param __fmt [IN] The format string.
* @param __flags [IN] Search and replace policy flags.
*
* @returns __out
* @throws an exception of type regex_error.
*/
template<typename _Out_iter, typename _Bi_iter,
typename _Rx_traits, typename _Ch_type,
typename _St, typename _Sa>
inline _Out_iter
regex_replace(_Out_iter __out, _Bi_iter __first, _Bi_iter __last,
const basic_regex<_Ch_type, _Rx_traits>& __e,
const basic_string<_Ch_type, _St, _Sa>& __fmt,
regex_constants::match_flag_type __flags
= regex_constants::match_default)
{
return regex_replace(__out, __first, __last, __e, __fmt.c_str(), __flags);
}
参考cppreference.std::string
上的这篇文章,这似乎是通过调用它c_str()
然后调用第二个重载,即带有const char *
参数的第二个重载,来实现第一个重载,即替换字符串的替换字符串。 . 这解释了观察到的行为。我找不到任何需要这种方法的东西。
进一步进入实际实施:
auto __len = char_traits<_Ch_type>::length(__fmt);
__out = __i->format(__out, __fmt, __fmt + __len, __flags);
因此,它确定替换字符串的长度并将替换字符串作为开始和结束迭代器传递到format()
.
这似乎应该是相反的方式,__fmt
保留为 a std::basic_string
,并将直接从它派生的迭代器传递到format()
.
更多推荐
std,regex
发布评论