Why doesn’t `std::initializer_list` provide a subscript operator?

According to Bjarne Stroustrup in Section 17.3.4.2 (p. 497) of The C++ Programming Language, 4th Edition: Unfortunately, initializer_list doesn’t provide subscripting. No further reason is given. My guess is that it’s one of these reasons: it’s an omission, or because the initializer_list class is implemented with an array and you’d have to do bounds checking … Read more

How can I implement the Iterable interface?

Iterable is a generic interface. A problem you might be having (you haven’t actually said what problem you’re having, if any) is that if you use a generic interface/class without specifying the type argument(s) you can erase the types of unrelated generic types within the class. An example of this is in Non-generic reference to … Read more

How can I check if an object is an iterator in Python?

In Python 2.6 or better, the designed-in idiom for such behavioral checks is a “membership check” with the abstract base class in the collections module of the standard library: >>> import collections >>> isinstance(‘ciao’, collections.Iterable) True >>> isinstance(23, collections.Iterable) False >>> isinstance(xrange(23), collections.Iterable) True Indeed, this kind of checks is the prime design reason for … Read more

UnsupportedOperationException in AbstractList.remove() when operating on ArrayList

I think you may be using the Arrays utility to get the List that you pass into that method. The object is indeed of type ArrayList, but it’s java.util.Arrays.ArrayList, not java.util.ArrayList. The java.util.Arrays.ArrayList version is immutable and its remove() method is not overridden. As such, it defers to the AbstractList implementation of remove(), which throws … Read more