4

The std::vector class has a convenient constructor which allows any input iterator for its parameter. I would like to implement a similar pattern in my own class, because the class needs to take in some collection when instantiated, but I would like to have the iterator over that collection for encapsulation purposes. One way that I thought of to do this is template-ing the whole class with the input iterator type, but that can't be what STL does, because vector is clearly only templated with the type being iterated over. Of course, one option is a templated generator function, but I'd really like to know how it's done by compilers that implement STL - somehow, the InputIterator type is a typename specific only to the constructor, even if constructors can't be templated.

(Yes, I have tried to look at vector.tpp but I could not understand it).

VF1
  • 1,594
  • 2
  • 11
  • 31
  • 3
    "even if constructors can't be templated." That's the thing - they *can* be, and it's exactly what `vector` does. – jrok Aug 07 '13 at 17:31
  • @jrok Well, that seems easy then. What is [this question](http://stackoverflow.com/questions/3960849/c-template-constructor) all about then? – VF1 Aug 07 '13 at 17:35
  • Template parameters of a constructor need to be deduced because there is no way to specify them explicitly. – jrok Aug 07 '13 at 17:37
  • @jrok In that case my question seems pretty trivial - is there any reason to keep it up? – VF1 Aug 07 '13 at 17:38
  • @VF1: Still a perfectly reasonable question, unambiguous and easy to find, and with a clear answer. If you delete it, someone else will ask it in the future. – MSalters Aug 08 '13 at 07:24

1 Answers1

6

Your class should have a templated constructor (templated on the iterator type):

class my_class {

    template <typename InputIterator>
    my_class(InputIterator first, InputIterator last) {
        // ...
    }

    // ...
};
Cassio Neri
  • 19,583
  • 7
  • 46
  • 68
  • I really wanted to try this but [this question](http://stackoverflow.com/questions/3960849/c-template-constructor) deterred me from doing so. – VF1 Aug 07 '13 at 17:34
  • @VF1 The important requirement the OP wants in that question is *I wish to have a non-template class with a template constructor **with no arguments***. This cannot be done because, as James' answer states, for a templated constructor, the template arguments must be deduced. – Praetorian Aug 07 '13 at 17:36
  • @VF1: The question in the reference you provided is about a templated constructor that **does not take any argument**. In your case it does take at least one (or more likely two). In any case, what I'm explaining in my post is exactly what the STL containers do. – Cassio Neri Aug 07 '13 at 17:38
  • @CassioNeri Yes - I get it now. I'd accept, but I have to wait 5 minutes. – VF1 Aug 07 '13 at 17:39
  • 1
    @VF1 When you implement this, also be aware of pitfalls like [this one](http://stackoverflow.com/questions/16427951/implicit-conversion-from-int-to-vector) where the templated constructor may turn out to be a better match than another constructor (that you'd intuitively except to be a better match) – Praetorian Aug 07 '13 at 17:41
  • 1
    @Praetorian thanks for the advice. What I'm currently working on only has one constructor, but I'll keep what you said in mind for the future. – VF1 Aug 07 '13 at 17:43