1

I have only found examples of regex_iterators being intialised as

regex_iterator::<string::iterator>

If I have a class which contains a sequence of characters, such as:

class fooString {
private:
  deque<bar> data;

  // ... other functionality ...
}

Where bar is a class or struct that is compatible with std::to_string

How would I make fooString and bar compatible with regex so that I could call

fooString myFoo = ...
regex e( /* some regex notation */);
regex_iterator::<fooString::iterator> regIt(myFoo.begin(), myFoo.end(), e);

...and be able to iterate over it as I would with a string?


I tried the following experiement:

    std::deque<int> testDeque { 4, 5, 5, 5, 6, 7 };
    std::regex e("5{1,3}");
    std::regex_iterator<std::deque<int>::iterator> 
    regIt(testDeque.begin(), testDeque.end(), e);

And get the compilation error

1>C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\regex(2271): error C2027: use of undefined type 'std::regex_traits<_Elem>'
with  
[  
   _Elem=int  
]

So I don't think the bidirectional iterator is enough since deque complies with that, I think the 'bar' object needs to be compatible. But I'm really not sure.

2
  • 1
    According to this std::regex_iterator reference, the iterator needs to be a bidirectional iterator. So if your iterator fulfills the requirements of a bidirectional iterator, you're good to go. Commented Oct 12, 2017 at 12:56
  • @Someprogrammerdude I think the data type Bar also needs to fullfill some particular property, but I don't know what it is. Commented Oct 12, 2017 at 15:02

1 Answer 1

2

fooString must satisfy the requirements of a bidirectional iterator. [bidirectional.iterators] cppreference's documentation is extremely close to the standard wording, so you'd be okay following it.

I will add the relevant portions here:

  • The iterator, r, must satisfy both operations --r and r++
    • these operations return a reference to an iterator of the same type
  • *r must return a reference to the type the iterator is iterating over.
    • a reference type according to what cppreference lists for iterator_traits

If your BidirectionalIterator::value_type is not a standard character type (anything you could make a std::basic_string with, e.g., char), you'll also need to specialize regex_traits, although this is going down quite the rabbit hole.

Sign up to request clarification or add additional context in comments.

5 Comments

I tried creating a simple test using regex_iterator<std::deque<uint32_t>::iterator> however the constructor complains that "regex(2271): error C2027: use of undefined type 'std::regex_traits<_Elem>' - with _Elem=unsigned int So unless I'm mistaken satifying the bidirectional iterator isn't enough - regex only works with some data types.
@Ian: Why uint32_t? You'll need a character type.
for my real situation I'm using a custom struct (Bar), I picked uint32_t because I thought it was a simple type to test with. I also tested with int with the same result. - What I'm really asking is how do I make a FooString containing a structure of Bars work? I think both the FooString and the Bar need some special sauce and while FooString might be the bidirectional iterator I don't know what it is for Bar.
@Ian: I have updated the post to discuss regex_traits, but I don't understand why you changed to int instead of a char it works fine with char
that regex_traits is exactly what I needed. Implementing custom behavior for types other than char and w_char will achieve what I need! Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.