c++ - How can i carry the implementation of a member of a class template with arguments by default out of boundary this class template? -


i work in vs 2010. i'm trying expand functionality , redefine functions of multimap container:

// test_multimap.h  #include <map>  namespace std {      template <typename tkey, typename tdata,              class compare = less<tkey>,             class alloc = allocator<pair<const tkey, tdata>>             >      class test_multimap: public multimap<tkey, tdata, compare, alloc>      {      public:           void clear()          {              multimap<tkey, tdata>::clear();          }           /*...*/ 

this works, if try carry out implementation of member functions, encounter problems:

// test_multimap.h  #include <map>  namespace std {      template <typename tkey, typename tdata,                class compare = less<tkey>,               class alloc = allocator<pair<const tkey, tdata>>          >     class test_multimap: public multimap<tkey, tdata, compare, alloc>     {     public:          void clear();         /*...*/    // test_multimap.cpp #include "stdafx.h" #include "test_multimap.h"  namespace std {      template <typename tkey, typename tdata,               class compare = less<tkey>,               class alloc = allocator<pair<const tkey, tdata>>              >     void test_multimap<tkey, tdata, compare, alloc>::clear()     {         multimap<tkey, tdata>::clear();     }   } 

in case error

c4519 (template arguments default can used in class template) in other cases set of different errors.

how can carry out implementation of template member functions??

just error suggests: don't use defaults when defining function , fine.

namespace std {      template <typename tkey, typename tdata,               class compare,               class alloc              >     void test_multimap<tkey, tdata, compare, alloc>::clear()     {         multimap<tkey, tdata>::clear();     }  } 

there 2 additional problems code:

  • as jbl says in comment, not idea inherit stl containers because have no virtual destructor. test_multimap appear work, tries use polymorphically , delete test_multimap through base class pointer run undefined behaviour.
  • adding classes std namespace forbidden standard (§17.6.4.2.1/1: the behavior of c++ program undefined if adds declarations or definitions namespace std or namespace within namespace std unless otherwise specified.). use different namespace.

Comments