Search code examples
c++templatesinheritancec++11const-iterator

Why can't I call a template base class constructor with a const_iterator?


For some reason, the following code gives the error Symbol 'TemplateBase' could not be resolved.:

template <typename T>
class TemplateBase
{
    TemplateBase(std::map<std::string, T>::const_iterator anIterator)
    { }
};

class SubClass : public TemplateBase<int>
{
    SubClass(std::map<std::string, int>::const_iterator anIterator) :
        TemplateBase<int>(anIterator) //Error: Symbol 'TemplateBase' could not be resolved.
    { }
};

Strangely, no error appears when I remove the ::const_iterator and only std::map<std::string, int> remains:

template <typename T>
class TemplateBase
{
    TemplateBase(std::map<std::string, T> aMap)
    { }
};

class SubClass : public TemplateBase<int>
{
    SubClass(std::map<std::string, int> aMap) :
        TemplateBase<int>(aMap) //No error.
    { }
};

Additionally, the following function gives no error either, so it really seems related to the combination of a template base class call with a const_iterator:

void function()
{
    std::map<std::string, int>::const_iterator anIterator;
    TemplateBase<int> aTemplateBase(anIterator); //No error
}

Is there some rule against using const_iterator as an argument for base class template constructors that I'm unaware of? Or is this a compiler bug?

I'm compiling with MinGW 64bit 4.9.0 on Windows 7, using C++11.


Solution

  • When you use a nested type that depends on a template type you need to use the typename keyword:

    TemplateBase(typename std::map<std::string, T>::const_iterator anIterator)
    { }