[C++] function to take both iterator and reverse_iterator

Started by
27 comments, last by coordz 16 years, 1 month ago
Quote:Exactly my point. Then what's the point of making this function general? Why not pass a random access iterator in the first place?


The point is that writing the function to work with any random access iterator is easier and simpler than writing the function just for vector::iterator and vector::const_iterator.
Advertisement
Quote:Original post by Ashkan
Exactly my point. Then what's the point of making this function general? Why not pass a random access iterator in the first place?

There is no single template argument you can use that specifies only random access iterators. You can add conditions in the function body to cause a compiler error if the type parameter is not a random access iterator, but that has the exact same effect as using random access iterator functionality that isn't in other iterator classes.
Quote:Original post by Nitage
You already have your answer:


template <typename T, typename Iter>
vector<T> myFunc(Iter beginIter, Iter endIter);

As I understand things this makes the function so general that I could pass in anything, even int's and float's. I assume from the discussion so far there is no way to limit what Iter can be? And that iterator and reverse_iterator do not have a common parent? Some sort of common parent would be ideal for my purpose.

Quote:Original post by SiCrane
Quote:Original post by Ashkan
Exactly my point. Then what's the point of making this function general? Why not pass a random access iterator in the first place?

There is no single template argument you can use that specifies only random access iterators. You can add conditions in the function body to cause a compiler error if the type parameter is not a random access iterator, but that has the exact same effect as using random access iterator functionality that isn't in other iterator classes.


Yes, you're right. It was a mistake on my part.
Quote:Original post by coordz
Quote:Original post by Nitage
You already have your answer:


template <typename T, typename Iter>
vector<T> myFunc(Iter beginIter, Iter endIter);

As I understand things this makes the function so general that I could pass in anything, even int's and float's. I assume from the discussion so far there is no way to limit what Iter can be? And that iterator and reverse_iterator do not have a common parent? Some sort of common parent would be ideal for my purpose.


Techinically yes, but you will likely write the function so that things like floats and ints will fail to compile. For instance, if you use the dereference operator then you will get a compile time error on ints and floats. In fact, the only data types that really support that operator are pointers and iterators. You've narrowed down the number of datatypes that can be used in your function successfully a great deal. As long as the container supports the functionality you desire you'll be fine.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

And one final question...... I understand this bit of code as it's just adding Iter to represent some iterator.
template <typename T, typename Iter>vector<T> myFunc(Iter beginIter, Iter endIter);

I don't understand how this code
template <typename Iterator>vector<typename Iterator::value_type> myFunc(Iterator begin, Iterator end)

is equivalent. Specifically, what is Iterator::value_type?
Here and here maybe?
Quote:Original post by rip-off
Here and here maybe?

Thanks for that. My poor google skills did not serve me well.... :-D
Quote:Original post by coordz
And one final question...... I understand this bit of code as it's just adding Iter to represent some iterator.
template <typename T, typename Iter>vector<T> myFunc(Iter beginIter, Iter endIter);

I don't understand how this code
template <typename Iterator>vector<typename Iterator::value_type> myFunc(Iterator begin, Iterator end)

is equivalent. Specifically, what is Iterator::value_type?


Iterator::value_type is the type of the value that dereferencing the iterator would give you, although using iterator_traits<Iterator>::value_type would be better.

You can also use Boost.Concepts to define what type of iterators you require.

template <typename ForwardIterator>vector<typename iterator_traits<ForwardIterator>::value_type> foo(ForwardIterator start, ForwardIterator end){   boost::function_requires<boost::ForwardIteratorConcept<ForwardIterator> >();   // ...}

--Michael Fawcett
Quote:Original post by mfawcett
Quote:Original post by coordz
template <typename Iterator>vector<typename Iterator::value_type> myFunc(Iterator begin, Iterator end)

is equivalent. Specifically, what is Iterator::value_type?


Iterator::value_type is the type of the value that dereferencing the iterator would give you, although using iterator_traits<Iterator>::value_type would be better.


This is also a handy way of disallowing regular pointers to be passed as iterators, as they of course do not have a value_type defined.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

This topic is closed to new replies.

Advertisement