Opened 21 years ago

Closed 19 years ago

#41 closed Feature Requests (None)

smart_ptr: (auto_ptr) constructor

Reported by: d-elf Owned by: Peter Dimov
Milestone: Component: smart_ptr
Version: None Severity:
Keywords: Cc:

Description

I'm wondering why the smart_ptr's (auto_ptr) 
constructor is defined as explicit?

In smart_ptr's documentation this constructor is not
explicit:

--cut--
template<typename Y>
shared_ptr(std::auto_ptr<Y>& r);
--cut--

but in source:

--cut--
template<typename Y>
      explicit shared_ptr(std::auto_ptr<Y>& r) {
         pn = new long(1); // may throw
         px = r.release(); // fix: moved here to stop 
leak if new throws
      }
--cut--
explicit shared_ptr(std::auto_ptr<T>& r) {
         pn = new long(1); // may throw
         px = r.release(); // fix: moved here to stop 
leak if new throws
      } 
--cut--

It would be nice to do for example following without 
initializing an shared_ptr instance by myself:

// START

void func_that_eats_both_ap_and_sp(const 
boost::shared_ptr<int>& sp)
{
 // ... 
}

int main(void)
{
 std::auto_ptr<int> ap(0);

 func_that_eats_both_ap_and_sp(ap);
}

// END

auto_ptr's constructor should not be the reason why 
this is defined as it is, since it is defined as 
explicit...

-d-elf

Change History (1)

comment:1 by Peter Dimov, 19 years ago

Status: assignedclosed
Logged In: YES 
user_id=305912

The constructor is explicit for safety reasons since it modifies 
the source auto_ptr.
Note: See TracTickets for help on using tickets.