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).

5
  • 3
    "even if constructors can't be templated." That's the thing - they can be, and it's exactly what vector does. Commented Aug 7, 2013 at 17:31
  • @jrok Well, that seems easy then. What is this question all about then? Commented Aug 7, 2013 at 17:35
  • Template parameters of a constructor need to be deduced because there is no way to specify them explicitly. Commented Aug 7, 2013 at 17:37
  • @jrok In that case my question seems pretty trivial - is there any reason to keep it up? Commented Aug 7, 2013 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. Commented Aug 8, 2013 at 7:24

1 Answer 1

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) {
        // ...
    }

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

6 Comments

I really wanted to try this but this question deterred me from doing so.
@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.
@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.
@VF1 When you implement this, also be aware of pitfalls like this one 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 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.
|

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.