I am coding a multithread program.
I have a multimap that will receive data from many threads.
Therefore I want to wrap the class in a derived class with overwitten members as necessary.
This is my code to do his:
1 2 3 4 5 6 7 8 9 10 11 12
template <typename T> class Border_map :
public std::multimap<T, RC>
{
std::mutex _mutex ;
public:
std::multimap<T, RC>::iterator insert ( const value_type& x ) // ERROR HERE
{
std::lock_guard<std::mutex> guard (_mutex) ;
return std::multimap<T, RC>::insert(x) ;
}
} ;
The compiler does not understand any of my attempts at describing the return type of the function insert. What should I write instead?
First of all, what is "RC"? It's not declared anywhere in your example.
Second, both iterator and value_type are dependent types, the correct syntax would be typename std::multimap<T, RC>::iterator insert(consttypename std::multimap<T, RC>::value_type& x )
Third, don't inherit from std::multimap, use composition.
@Cubbi
Thanks. it works. I was not familiar with this use of keyword typename. As far as I understood what I read since your reply, we must use it anywhere the compiler cannot understand from te context that we are actually describing a type name. Correct?
why did you advise not to inherit from std::multimap?
I chose heritage because I did not want to overwrite all the methods I will be using, but only those for which I need a lock. Is there anything wrong in this logic? or is there something more important that I have ignored?
@L B
I thought it was in the scope, but the compiler did not accept a line like this:iterator insert(...)
why did you advise not to inherit from std::multimap?
Because it is not a polymorphic class. Specifically, its destructor is public nonvirtual. While you technically can do this, public inheritance from such classes is bad design.