std::map::const_iterator template compilation error

前端 未结 3 1112
灰色年华
灰色年华 2020-12-20 00:56

I have a template class that contains a std::map that stores pointers to T which refuses to compile:

template 
class Foo
{
public         


        
相关标签:
3条回答
  • 2020-12-20 01:34

    You need:

    typename std::map<int, T*>::const_iterator begin() const { return items.begin(); }
    

    Or simpler

    typedef typename std::map<int, T*>::const_iterator const_iterator;
    const_iterator begin() const { return items.begin(); }
    

    This is because const_iterator is dependent name on T so you need to tell compiler that it is actually type.

    0 讨论(0)
  • 2020-12-20 01:57

    Use typename:

    typename std::map<int, T*>::const_iterator begin() const ...
    

    When this is first passed by the compiler, it doesn't know what T is. Thus, it also doesn't know wether const_iterator is actually a type or not.

    Such dependent names (dependent on a template parameter) are assumed to

    • not be types unless prefixed by typename
    • not to be templates unless directly prefixed by template.
    0 讨论(0)
  • 2020-12-20 01:57

    You need typename:

    typename std::map<int, T*>::const_iterator begin() const { return items.begin(); }
    
    0 讨论(0)
提交回复
热议问题