how to deduce template parameters for the end regex_token_iterator on the begin one?

Viewed 63

How to write the following so-called mini-reproducable example in which the begin iterator may be constract either on the base of std::string_view or std::string.

So, let's say that we have the following programme:

#include <iostream>
#include <string>
#include <regex>
#include <iterator>
#include <string_view>

int main() {
  std::string_view sv("QWERTY");
  std::string str("QWERTY");
  std::regex rx(R"([\w])");
  for (auto it = std::regex_token_iterator(str.cbegin(), str.cend(), rx, 0); it != std::sregex_token_iterator(); it++) {
    std::cout << *it << std::endl;
  }
  return 0;
}

I wonder, how to specify the last std::regex_token_iterator<???>() on the base of the type of it in order not to be forced to change the code depending on whether it runs over std::string_view or std::string.

I have tried to specify like decltype(it)::value_type but I have got an error that there is no such a member type.

1 Answers

You're thinking way too deeply. You don't need the template parameters; the type you're trying to test against is the same type as it. So just use that:

it != decltype(it)()
Related