0

The advice I've always heard is that inheritance from anything in the standard is frowned upon. I should have a has-a not is-a relationship to it.

I think that is not true with respect to iterators, right? We must inherit from iterator in our boilerplate right?

Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
  • 1
    You don't *need* to, you can define all the necessary typedefs yourself. – milleniumbug May 04 '16 at 13:40
  • You do not have to inherit from `std::iterator` but doing so is silly as you have to type all the boilerplate then. – NathanOliver May 04 '16 at 13:40
  • And I always say that all generalizations are wrong (including this one). There are reasons not to inherit from std classess. There are resons to inherit from them. However, since you haven't given any details of your particular proble, I can't say if your case is valid for inheritance or not. – SergeyA May 04 '16 at 13:40
  • @SergyA This isn't a specific problem. I just wanted to know if it was acceptable practice to inherit from `iterator`. – Jonathan Mee May 04 '16 at 13:41
  • 2
    You should read the second answer to the linked duplicate: http://stackoverflow.com/a/22211034/14065 which explains why you should no longer do this. – Martin York May 04 '16 at 13:52
  • @LokiAstari So in reading that it seems to indicated that `iterator` will be deprecated in C++17 and that I need to define my own typedefs. But as you point out in [your own answer](http://stackoverflow.com/a/6471048/2642059) `iterator` types save us from a ton of tedium. Is that tedium now being forced upon us? – Jonathan Mee May 04 '16 at 14:16
  • @JonathanMee: I only got 2 votes for a reason :) -> Looks like the tedium is being pushed to the ` iterator_traits` class. So if you don't need the types in your iterator then don't specify them. If you need them then you need to implement the ` iterator_traits` specialization for your iterator (but only if you need it). – Martin York May 04 '16 at 14:19
  • @LokiAstari Woah, woah, I thought that [`iterator_traits`](http://en.cppreference.com/w/cpp/iterator/iterator_traits) was used to unpack iterator information to determine how a function that took an iterator should behave. Are you saying this should somehow be used in my iterator boilerplate? – Jonathan Mee May 04 '16 at 14:27
  • 1
    @JonathanMee: Currently `iterator_traits` takes advantage of the fact that iterators have the types in them (the default implementation). But the alternative way is to specialize iterator_traits for your type. Its 6 of one half a dozen of the other. The STL accesses the type information about your iterator via the iterator_traits (I believe) not an expert on this part. But the real point is SFINAE means you don't need to specify the types if you are not using them. – Martin York May 04 '16 at 14:39
  • @JonathanMee: I tried to find an question about this but could not. It may be worth asking a question on when/how to specialize iterator_traits (I don't think I can speak definitively on the subject and it would be good to get more people to chime in). – Martin York May 04 '16 at 14:52
  • @LokiAstari http://stackoverflow.com/q/37031805/2642059 feel free to edit to make that question more logical if I have misinterpreted you. – Jonathan Mee May 04 '16 at 15:15

0 Answers0