Boost logo

Boost :

Subject: [boost] boost::assign::map_list_of and references?
From: David Hagood (david.hagood_at_[hidden])
Date: 2012-09-08 16:10:37


I was trying to initialize a map using map_list_of, and ran into what I
consider an undesirable behavior:

class stooge
{
   public:
     stooge();
     stooge(const stooge &);
     stooge &operator =(const stooge &);
};

typedef std::map<std::string,const stooge &> MyMap_t;

extern const stooge larry,moe,curly;

MyMap_t mymap = boost::assign::map_list_of
("larry",larry)
("moe",moe)
("curly",curly);

The above leaves mymap full of bogus references - it seems map_list_of
is creating temporary copies of the objects, rather than taking their
references, and then the map takes references to those temporaries, and
blooley!

Changing the assignment to:
MyMap_t mymap = boost::assign::map_list_of<std::string, const stooge &>

doesn't change anything.

(and for various reasons, making stooge noncopyable is not an option).

This is under gcc 4.5 i686 target.

Am I "doing it wrong" or is this an issue with map_list_of?


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk