Assignement operator, templates, double associative array

M

mscava

I'm building DataManager<T>. A class where shared data will be stored.
One of things useful to implement is garbage collection. But it still
gives me this error:

stl_algo.h:1076: error: non-static const member `const std::string
DataMapPair::first', can't use default assignment operator

Here's my code:

typedef std::map< std::string, std::pair< bool, CountedPtr<T> > >
DataMap;
typedef std::pair< std::string, std::pair< bool, CountedPtr<T> > >
DataMapPair;

DataMap dataMap_;

template <typename T> bool DataManager<T> ::
IsGarbage( const DataMapPair& dmPair )
{
return ( dmPair.second.first && dmPair.second.second.IsUnique() );
}

template <typename T> void DataManager<T> ::
CollectGarbage()
{
dataMap_.erase( remove_if( dataMap_.begin(), dataMap_.end(),
IsGarbage ), dataMap_.end() );
}

Can anyone tell how should I oveload that operator=? Or should I do
something else?
 
J

John Harrison

I'm building DataManager<T>. A class where shared data will be stored.
One of things useful to implement is garbage collection. But it still
gives me this error:

stl_algo.h:1076: error: non-static const member `const std::string
DataMapPair::first', can't use default assignment operator

Here's my code:

typedef std::map< std::string, std::pair< bool, CountedPtr<T> > >
DataMap;
typedef std::pair< std::string, std::pair< bool, CountedPtr<T> > >
DataMapPair;

DataMap dataMap_;

template <typename T> bool DataManager<T> ::
IsGarbage( const DataMapPair& dmPair )
{
return ( dmPair.second.first && dmPair.second.second.IsUnique() );
}

template <typename T> void DataManager<T> ::
CollectGarbage()
{
dataMap_.erase( remove_if( dataMap_.begin(), dataMap_.end(),
IsGarbage ), dataMap_.end() );
}

Can anyone tell how should I oveload that operator=? Or should I do
something else?

remove_if tries to rearrange the sequence it is given. std::map's are
stored in a fixed order (i.e. they are ordered by a key) and they don't
like it when you try to rearrange them. That is what the error message
means.

Instead of trying fancy stuff in your CollectGarbage method you should
just write a loop through the entries of the map and delete any that are
garbage.

john
 
M

mscava

remove_if tries to rearrange the sequence it is given. std::map's are
stored in a fixed order (i.e. they are ordered by a key) and they don't
like it when you try to rearrange them. That is what the error message
means.

Instead of trying fancy stuff in your CollectGarbage method you should
just write a loop through the entries of the map and delete any that are
garbage.

john

Thank you very much... I should have known.
 
A

Andre Kostur

(e-mail address removed) wrote in @a75g2000cwd.googlegroups.com:


Out of curiosity, any reason why the second line isn't:

typedef DataMap::value_type DataMapPair;

? Or simply use DataMap::value_type directly?
 
M

mscava

Out of curiosity, any reason why the second line isn't:
typedef DataMap::value_type DataMapPair;

? Or simply use DataMap::value_type directly?

I'm still not so IN the STL as I should be... And I didn't know
construction like this exists. Currently reading a book about STL form
Josuttis, so hopefully I'll get better.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top