2

So I have SkipList.hpp which has a nested templated class called SkipListIterator.

//SkipList.hpp
template <class Key_t, class Mapped_t>
class SkipList {
    template <bool isConst, bool isReverse>
    class SkipListIterator {
        ...
    }
    ...
}

In my Map.hpp, I want to make typedefs for the different types of iterators. What I tried to do was the following:

//Map.hpp

#include "SkipList.hpp"
template <class Key_t, class Mapped_t>
class Map {
    typedef typename SkipList<Key_t, Mapped_t>::SkipListIterator<false, false> iterator;
    typedef typename SkipList<Key_t, Mapped_t>::SkipListIterator<true, false> const_iterator;
    typedef typename SkipList<Key_t, Mapped_t>::SkipListIterator<false, true> reverse_iterator;
    typedef typename SkipList<Key_t, Mapped_t>::SkipListIterator<true, true> const_reverse_iterator;
    ...
}

This does not work, and g++ gives me the following error:

error: non-template 'SkipListIterator' used as template
typedef typename SkipList<Key_t, Mapped_t>::SkipListIterator<false, false> iterator
                                            ^
935
  • 59
  • 1
  • 10

1 Answers1

3

This works with gcc 6.3.1:

template <class Key_t, class Mapped_t>
class SkipList {
    template <bool isConst, bool isReverse>
    class SkipListIterator {
    };
};

template <class Key_t, class Mapped_t>
class Map {
    typedef typename SkipList<Key_t, Mapped_t>::template SkipListIterator<false, false> iterator;
    typedef typename SkipList<Key_t, Mapped_t>::template SkipListIterator<true, false> const_iterator;
    typedef typename SkipList<Key_t, Mapped_t>::template SkipListIterator<false, true> reverse_iterator;
    typedef typename SkipList<Key_t, Mapped_t>::template SkipListIterator<true, true> const_reverse_iterator;
};

When working with templates, the compiler often needs extra help to figure out what it is type, a template, or a non-type.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148