127. auto_ptr<> conversion issues

Section: 99 [auto.ptr] Status: TC1 Submitter: Greg Colvin Opened: 1999-02-17 Last modified: 2016-02-10

Priority: Not Prioritized

View all other issues in [auto.ptr].

View all issues with TC1 status.

Discussion:

There are two problems with the current auto_ptr wording in the standard:

First, the auto_ptr_ref definition cannot be nested because auto_ptr<Derived>::auto_ptr_ref is unrelated to auto_ptr<Base>::auto_ptr_ref. Also submitted by Nathan Myers, with the same proposed resolution.

Second, there is no auto_ptr assignment operator taking an auto_ptr_ref argument.

I have discussed these problems with my proposal coauthor, Bill Gibbons, and with some compiler and library implementors, and we believe that these problems are not desired or desirable implications of the standard.

25 Aug 1999: The proposed resolution now reflects changes suggested by Dave Abrahams, with Greg Colvin's concurrence; 1) changed "assignment operator" to "public assignment operator", 2) changed effects to specify use of release(), 3) made the conversion to auto_ptr_ref const.

2 Feb 2000: Lisa Lippincott comments: [The resolution of] this issue states that the conversion from auto_ptr to auto_ptr_ref should be const. This is not acceptable, because it would allow initialization and assignment from _any_ const auto_ptr! It also introduces an implementation difficulty in writing this conversion function -- namely, somewhere along the line, a const_cast will be necessary to remove that const so that release() may be called. This may result in undefined behavior [7.1.5.1/4]. The conversion operator does not have to be const, because a non-const implicit object parameter may be bound to an rvalue [13.3.3.1.4/3] [13.3.1/5].

Tokyo: The LWG removed the following from the proposed resolution:

In 23.15.4 [meta.unary], paragraph 2, and 23.15.4.3 [meta.unary.prop], paragraph 2, make the conversion to auto_ptr_ref const:

template<class Y> operator auto_ptr_ref<Y>() const throw();

Proposed resolution:

In 23.15.4 [meta.unary], paragraph 2, move the auto_ptr_ref definition to namespace scope.

In 23.15.4 [meta.unary], paragraph 2, add a public assignment operator to the auto_ptr definition:

auto_ptr& operator=(auto_ptr_ref<X> r) throw();

Also add the assignment operator to 23.15.4.3 [meta.unary.prop]:

auto_ptr& operator=(auto_ptr_ref<X> r) throw()

Effects: Calls reset(p.release()) for the auto_ptr p that r holds a reference to.
Returns: *this.