Opened 13 years ago

Closed 13 years ago

#3022 closed Bugs (invalid)

Automatic conversion from Y* to boost::shared_ptr<Y> with std::pair

Reported by: koelsch@… Owned by: Peter Dimov
Milestone: Component: smart_ptr
Version: Boost 1.39.0 Severity: Problem
Keywords: implicit conversion shared_ptr Cc:

Description

The assignment of std::pair<X,Y*> to std::pair<X,boost::shared_ptr<Y> > compiles without warning and implicitly converts a Y* to a boost::shared_ptr<Y>. I guess this has to do with:

template<class T> class shared_ptr { ... template<class Ap> typename boost::detail::sp_enable_if_auto_ptr< Ap, shared_ptr & >::type operator=( Ap r ) ... };

This behavior was witnessed under VC2005, VC2008, and gcc4.0.1.

A minimal program demonstrating an evil consequence of this follows:

int main() {

int i = 2; boost::shared_ptr<int> j( new int );

boost::shared_ptr<int> k = &*j; does not compile std::pair<int,boost::shared_ptr<int> > v( i, &*j ); does not compile std::pair<int,boost::shared_ptr<int> > v = std::make_pair( i, &*j ); However, this compiles kabum

}

Regards,

Tobias

Change History (4)

comment:1 by Peter Dimov, 13 years ago

This is caused by std::pair's converting constructor:

template<class U, class V> pair<T1, T2>::pair( U const & u, V const & v ):
  first( u ), second( v ) {}

and, unfortunately, there is nothing shared_ptr can do to stop it.

comment:2 by Peter Dimov, 13 years ago

Sorry, I meant:

template<class U, class V>
  pair<T1, T2>::pair( pair<U, V> const & r ):
  first( r.first ), second( r.second ) {}

The initialization of second (of type shared_ptr) from r.second (or type int*) is explicit and as such, it does succeed.

comment:3 by koelsch@…, 13 years ago

Sorry, I did not think such a constructor exists. I'll check that the next time. Should I resolve the Bug or is this usually done by the maintainer?

Tobias

comment:4 by Peter Dimov, 13 years ago

Resolution: invalid
Status: newclosed
Note: See TracTickets for help on using tickets.