Can I create an empty range (a couple of iterators) without the underlying container object?

I have a class related to the following:

struct Config { using BindingContainer = std::map<ID, std::vector<Binding>>; using BindingIterator = BindingContainer::mapped_type::const_iterator; boost::iterator_range<BindingIterator> bindings(ID id) const; private: BindingContainer m_bindings; }; 

Since the ID passed in bindings() may not exist, I need to be able to represent the value “no bindings” in the domain of the return type.

I do not need to distinguish between an unknown ID and an ID mapped to an empty vector , so I was hoping to achieve this using the interface as described above and return the empty range to the default -designed iterators. Unfortunately, although a ForwardIterator is DefaultConstructible [C ++ 11 24.2.5 / 1], the result of comparing a singular iterator is undefined [24.2.1 / 5], so without a container it seems that this is not possible.

I could change the interface, for example, wrap iterator_range in boost::optional or return a vector value instead; however, the subscriber is still a little uncomfortable, and the latter has undesirable overhead.

Another option is to save the statically selected empty vector and return its iterators. Overhead would not be problematic in this case, but I would like to avoid it if I can.

Adapting the map iterator to get comparable default iterations is an option, although it seems too complicated ...

Are there any other parameters that would support returning an empty range when there is no base container?

(By the way, I am sure that some time ago I read a working document or an article on creating empty ranges for a standard type of container when there is no container object, but cannot find anything now.)

(Note. I am limited by C ++ 11, although I would be wondering if there is another approach that requires later functions.)

+7
c ++ boost c ++ 11
source share
1 answer

No no. Your options as you suggest. Personally, I probably go with the idea of ​​capturing a pair of iterators from a static empty vector; I can’t imagine what conditional “overhead” will be involved here, in addition to a few extra bytes in the image of your process.

  • Is this the only iterator, and if so, can I compare it with another?
  • Comparing default iterators with the == operator

And that has not changed in either C ++ 14 or C ++ 17 (so far).

+2
source share

All Articles