Why use typedef in this template?

When I compile this code in Visual Studio 2005:

  template <class T>
  class CFooVector : public std::vector<CFoo<T>>
  {
  public:
    void SetToFirst( typename std::vector<CFoo<T>>::iterator & iter );
  };

  template <class T>
  void CFooVector<T>::SetToFirst( typename std::vector<CFoo<T>>::iterator & iter )
  {
    iter = begin();
  }

I get the following errors:

c:\home\code\scantest\stltest1\stltest1.cpp(33) : error C2244:     'CFooVector<T>::SetToFirst' : unable to match function definition to an existing declaration
    c:\home\code\scantest\stltest1\stltest1.cpp(26) : see declaration of 'CFooVector<T>::SetToFirst'
    definition
    'void CFooVector<T>::SetToFirst(std::vector<CFoo<T>>::iterator &)'
    existing declarations
    'void CFooVector<T>::SetToFirst(std::_Vector_iterator<_Ty,_Alloc::rebind<_Ty>::other> &)'

If I add a typedef to the CFooVector template, I can get the code to compile and work:

  template <class T>
  class CFooVector : public std::vector<CFoo<T>>
  {
  public:
    typedef typename std::vector<CFoo<T>>::iterator FooVIter;
    void SetToFirst( FooVIter & iter );
  };

  template <class T>
  void CFooVector<T>::SetToFirst( FooVIter & iter )
  {
    iter = begin();
  }

My question is: why does typedef work when using an ad with an open ad 'typename std::vector>::iterator'does not work?

+3
source share
5 answers

It also compiles and reveals the source of the confusion of VC ++ - the type of dispenser. Apparently, outside the class, VS selects different default values. Or maybe he cannot recognize them as one and the same.

Compiles VS2008 (as is) and VS2003 (with a space between →)

template <class T>
class CFoo
{
public:
    T m_t;
};

template <class T>
class CFooVector : public std::vector<CFoo<T>>
{
public:
    void SetToFirst(typename std::vector<CFoo<T>, typename CFooVector::_Alloc>::iterator & iter);

};

template <class T>
void CFooVector<T>::SetToFirst( typename std::vector<CFoo<T>, typename CFooVector::_Alloc>::iterator & iter )
{
    iter = begin();
}

GCC 3.4 this- > begin() , ... MS- , ...

+5

, typedef, typename.

, , ( , -, :: , , ( int, ), .

typename, , .

typedef typename typedef, typedef . , typename typedef.

, , std::vector<CFoo<T>>::iterator - .

, FooVIter , typedef. Typedefs .

+2

typename .

:

>>

, ,

operator>>

, typename. , ?

:

template <typename T>

template <class T>

, STL, , .

STL .

+1

, , -

template <class T>
class CFoo
{
public:
T m_t;
};

template <class T>
class CFooVector : public std::vector< CFoo<T> >
{
public:
void SetToFirst( typename std::vector<CFoo<T> >::iterator & iter );
};

template <class T>
void CFooVector<T>::SetToFirst( typename std::vector<CFoo<T> >::iterator & iter )
{
iter = begin();
}

std::vector<CFoo<T>> std::vector<CFoo<T> >, (gcc, ) → .

0

: VS2008 , std:: vector SetToFirst():

  template <class T>
  class CFooVector : public std::vector< CFoo<T> >
  {
  public:
      void SetToFirst( typename std::vector< CFoo<T> >::iterator & iter );
  };

  template <class T>
  void CFooVector<T>::SetToFirst( typename /*std::*/vector< CFoo<T> >::iterator & iter )
  {
    iter = begin();
  };

, , . , , "using namespace std;" ...

, .

0

Source: https://habr.com/ru/post/1714100/


All Articles