111111
111111

Reputation: 16168

Specifying template parameter with a Typedef

I want to able to pass two joined iterators as one to take advantage of some stl like algorithms (such as TBB) so I am making a custom iterator that joins them but am hitting some stumbling blocks.

I need to specialize iterator, however it won't let me generically specify a template parameter.

Like so:

template<typename IT1, typename IT2>
struct multi_iter : public std::iterator<
                            std::output_iterator_tag,
                            std::pair<IT1::value_type&, IT2::value_type&> >
{
.
:

However it will let me do this, but this is not what I am after

template<typename IT1, typename IT2>
struct multi_iter : public std::iterator<
                            std::output_iterator_tag,
                            std::pair<int&, int&> >
{
.
:

I get this error

multi_iter.cpp:12:53: error: template argument 2 is invalid
multi_iter.cpp:12:55: error: template argument 2 is invalid
multi_iter.cpp:12:55: error: template argument 4 is invalid
multi_iter.cpp:12:55: error: template argument 5 is invalid
.
:

I do have the std::pair

Any help would be greatly appreciated.

Thanks

Upvotes: 2

Views: 781

Answers (3)

jpalecek
jpalecek

Reputation: 47760

template<typename IT1, typename IT2>
struct multi_iter : public std::iterator<
                            std::output_iterator_tag,
                            std::pair<IT1::value_type&, IT2::value_type&> >

IT1::value_type is dependent on a type parameter and is a type, so it needs to be designated by typename keyword:

template<typename IT1, typename IT2>
struct multi_iter : public std::iterator<
                            std::output_iterator_tag,
                            std::pair<typename IT1::value_type&, typename IT2::value_type&> >

BTW if you want to "zip" two iterators (that is, iterate two sequences {1, 2} and {"a", "b"}, as (1, "a"), then (2, "b")), have a look at the zip_iterator from the boost.iterators library.

Upvotes: 1

Andr&#233;s Senac
Andr&#233;s Senac

Reputation: 851

Have you tried this?

template<typename IT1, typename IT2>
struct multi_iter : public std::iterator<
                            std::output_iterator_tag,
                            std::pair< typename IT1::value_type&, typename IT2::value_type& > >
{
.
:

Upvotes: 1

K-ballo
K-ballo

Reputation: 81379

value_type is a dependent type on IT1, so you have to specify typename there

typename IT1::value_type

Upvotes: 3

Related Questions