-1

I have class:

template <typename _T, typename _E>
class CRoute
{ 
  public:
    CRoute& Add(const _T & u1 , const _T & u2 , const _E & e);
    ...
  private:
    map < _T, vector<pair<_T,_E>> > graf;
};

Add function have to assign elements to graf;

BUT! I can not use default constructor of _T and _E types. Because of this I can't do following:

template <typename _T, typename _E>
CRoute<_T,_E>& CRoute<_T,_E>::Add(const _T & u1 , const _T & u2 , const _E & e){      
     graf[u1].emplace_back( u2 , e );
     return *this;
}

I can use only copy constructor. How can I make it?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Dmitriy
  • 77
  • 1
  • 1
  • 5

1 Answers1

0

You could take inspiration from std::map::emplace_back and have your own Add function work the same way, taking arguments for constructing _T and _E objects, rather than already-constructed instances of those objects.

Otherwise your only option is to take rvalue references and hope your types are movable.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055