$include_dir="/home/hyper-archives/boost-users/include"; include("$include_dir/msg-header.inc") ?>
From: Bill Buklis (boostuser_at_[hidden])
Date: 2007-08-14 13:57:14
> -----Original Message-----
> From: boost-users-bounces_at_[hidden] [mailto:boost-users-
> bounces_at_[hidden]] On Behalf Of e r
> Sent: Monday, August 13, 2007 6:23 PM
> To: boost-users_at_[hidden]
> Subject: [Boost-users] ptr_map<int, T> where T has no default constructor
> because its private members are const
> 
> hi,
> 
> i can build a map<int,A*>, but not a boost::ptr_map<unsigned int,A>.
> could someone please help?
> 
> thank you,
> 
> e.
> 
> 
> #include <boost/ptr_container/ptr_map.hpp>
> 
> using std::map;
> using std::cout;
> using std::endl;
> 
> class A{
> 	public:
> 		A(double x_):x(x_){};
> 		const double x;
> };
> 
> int main(){
> 
> 	map<int,A*> map_A;	//cannot use map<int,A>
>               				//because A() not defined
> 				//why? because A has const members.
> 
> 	A a(0.0);
> 	map_A[0]=new A(0.0);//fine
> 
> 
> 	boost::ptr_map<unsigned int,A> ptr_map_A;
> 	ptr_map_A[0]=new A(0.0);//not fine
> 
> 	return 0;
> };
I don't know for sure, but this may be disallowed for exception safety. In
any case this should work:
unsigned int	key = 0;
ptr_map_A.insert(key, new A(0.0));
"insert" also has the advantage in that the map doesn't have to create an
empty value or null-pointer entry) before the assignment.
-- Bill --