I'm trying to convert this function to a template.
std::vector<std::smatch> RegexMatchAll(const std::string& str, std::string regex)
{
std::vector<std::smatch> result;
std::regex re(regex);
auto words_begin = std::sregex_iterator(str.begin(), str.end(), re);
auto words_end = std::sregex_iterator();
for (std::sregex_iterator i = words_begin; i != words_end; ++i) {
std::smatch match = *i;
result.emplace_back(match);
}
return result;
}
So far what I have already done:
template <typename T>
auto RegexMatchAll(const std::basic_string<T>& str, const std::basic_string<T>& regex)
{
using iter = typename std::basic_string<T>::const_iterator;
std::basic_regex<T> re(regex);
std::vector<std::match_results<iter>> result;
auto words_begin = iter(str.begin(), str.end(), re);
auto words_end = iter();
for (iter i = words_begin; i != words_end; ++i) {
std::match_results<iter> m = *i;
result.emplace_back(m);
}
return result;
}
I'm getting some errors like:
<function-style-cast>': cannot convert from 'initializer list' to 'iter
'words_begin': cannot be used before it is initialized
And I'm calling the template as:
auto matches = RegexMatchAll(str, R"(...)"s);
auto matches = RegexMatchAll(wstr, LR"(...)"s);
Your template function tries to use
string::const_iterator
where the original usedstd::sregex_iterator
. That doesn't make any sense - besides the word "iterator" in their names, the two classes have absolutely nothing in common.You probably meant something like
and then use
regex_iter
where you now useiter
.I dont understand what you mean?
I'm compiling with visual studio 2019
Hi @Marcus ,
This code will cause C2440 error, I suggest you to read this issue.
Sign in to comment