Search code examples
c++classtemplatesiteratorclass-template

Using template value inside nested class


I have the following class definition:

template <int B>
class SparseBitvector
{
    typedef typename std::vector<std::bitset<B>> BitsetVector;

public:
    class iterator: public std::iterator<...>
    {
        public:
            explicit iterator(..., BitsetVector::iterator* _chunksIt, ...) {...}
    }
}

At compilation I get this error:

/project/powerset/src/powerset/sparse_bitvector.h:96:125: error: 'std::BitsetVector<std::bitset<(long unsigned int)B>, std::allocator<std::bitset<(long unsigned int)B> > >::iterator' is not a type
         explicit iterator(SortedStaticSet<EHRule> *_offsets, std::vector<std::bitset<B>> *_chunks, SetElement* _offssetsIt, BitsetVector::iterator* _chunksIt, int _indInBitset)

Is it allowed to use template value inside a nested class?

If not, is there some other error I am making?


Solution

  • You need to use typename keyword for depended types. Here for example

    typename BitsetVector::iterator* _chunksIt
    //^^^^^^
    

    Secondly, there is no typename needed for the template type alias

    typedef std::vector<std::bitset<B>> BitsetVector;
    // or better use
    // using BitsetVector = std::vector<std::bitset<B>>;
    

    The minimal example for the above have considered as follows:

    #include <vector>
    #include <iterator>
    #include <bitset>
    
    template <int B>
    class SparseBitvector
    {
       typedef std::vector<std::bitset<B>> BitsetVector;
    
    public:
       class iterator : public std::iterator<std::input_iterator_tag, BitsetVector>
       {
       public:
          explicit iterator(std::input_iterator_tag, BitsetVector, typename BitsetVector::iterator* _chunksIt)
          {
          }
       };
    };