Search code examples
c++std-pairtemplate-classes

C++ error declaring std::pair inside template class


I want to avoid using the std::pair() constructor or std::make_pair() functions while inserting into a map. I also want to know the success status of the insert operation, so I cannot use operator[]. I tried the following code but it produces a compile error.

template<typename TKey, typename TVal>
class Map
{
   private :
      std::map<TKey, TVal> m_holder;

   public :
      bool insert(TKey key, TVal val)
      {
         std::pair<std::map<TKey, TVal>::iterator, bool> ret;
         /* ret = m_holder.insert(std::make_pair(key, val)); */
         return 0;
      }
};

int main()
{
   return 0;
}

Error :

Hello.cpp: In member function `bool Map<TKey, TVal>::insert(TKey, TVal)':
Hello.cpp:13: error: type/value mismatch at argument 1 in template parameter list for `template<class _T1, class _T2> struct std::pair'
Hello.cpp:13: error:   expected a type, got ` std::map<TKey,TVal,std::less<_Key>,std::allocator<std::pair<const _Key, _Tp> > >::iterator'
Hello.cpp:13: error: invalid type in declaration before ';' token

Help me solve the problem.


Solution

  • std::pair<typename std::map<TKey, TVal>::iterator, bool> ret;
    //        ~~~~~~~^