Opened 14 years ago

Closed 14 years ago

#2951 closed Bugs (fixed)

Unwanted implicit conversion from std::auto_ptr to boost:;shared_ptr

Reported by: William Tanenbaum <wmtan@…> Owned by: Peter Dimov
Milestone: Boost 1.39.0 Component: smart_ptr
Version: Boost 1.38.0 Severity: Regression
Keywords: implicit conversion shared pointer Cc:

Description

In boost 1.38, the explicit keyword was dropped from this constructor template in shared_ptr.hpp:

template<class Ap> shared_ptr( AP r, typename boost::detail::sp_enable_if_auto_ptr<Ap, int>::type = 0 ): px( r.get() ), pn() {

typename Ap::element_type * tmp = r.get(); pn = boost::detail::shared_count( r ); boost::detail::sp_enable_shared_from_this( pn, tmp, tmp );

}

The explicit keyword was there in boost 1.34. This causes implicit conversions from auto_ptr to shared_ptr, which is undesirable.

Change History (5)

comment:1 by Peter Dimov, 14 years ago

It should only allow implicit conversions from auto_ptr _rvalues_. Is this a problem? Do you have an example?

comment:2 by wmtan@…, 14 years ago

It is definitely implicitly converting from an auto_ptr lvalue. We are using gcc3.4.5 (32 bit). We will be converting to gcc4.3 shortly, so I will check if the problem exists in gcc4.3.

I will get back to you with an example shortly.

comment:3 by wmtan@…, 14 years ago

The following code compiles and runs. An auto_ptr lvalue is implicitly converted to a shared_ptr. This should not happen. This happens in gcc4.3 and gcc3.4, with no compiler options other than -I to include the boost header.

#include <memory>

#include <iostream>

#include "boost/shared_ptr.hpp"

struct X {

int i;

};

void f(boost::shared_ptr<X> sp) {

std::cout << sp->i << std::endl;

}

int main() {

std::auto_ptr<X> ap(new X); ap->i = 1; f(ap);

}

~

comment:4 by Peter Dimov, 14 years ago

(In [52454]) Bring back "explicit" on the auto_ptr rvalue constructor. Refs #2951.

comment:5 by Peter Dimov, 14 years ago

Resolution: fixed
Status: newclosed

Merged to release in [52472].

Note: See TracTickets for help on using tickets.