2012/12/20 Olaf van der Spek <
olafv...@gmail.com>:
> Op woensdag 19 december 2012 12:01:02 UTC+1 schreef Daniel Krügler het
> volgende:
>
>> Please take a look at
>>
>>
http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-closed.html#1031
>>
>> If you have answers to the questions asked then I suggest to create
>> a proposal paper. Ensure that you reference LWG issue 1031 and expect
>> that people will ask critical questions in regard to invariants and
>> implement-ability.
>
>
> I think this request is somewhat different. The request is not to transfer
> ownership to unique_ptr, but for unique_ptr to hold a (one) reference to the
> shared object.
Yes, the requester has made his point clear after I presented the
issue and the question,
but this is not the only possible interpretation (see my quoted
issue). To the contrary
I would assume that any implicit conversion from shared_ptr to unique_ptr would
*transfer* ownership, because that is the main intention of unique_ptr
and the reason
for naming it that way.
It is just the fact that the generic deleter support and the generic
pointer support of
unique_ptr can be used to support very different models. Nonetheless I
consider such
models as very special. There is no reason that the standard library
should support
such special scenarios *directly*.
> struct C
> {
> C(P&& p) : p_(p) { }
> private:
> P p_;
> }
>
> What should P be here? If it's unique_ptr, I can't store a shared_ptr. If
> it's a shared_ptr, I can't store a raw or unique_ptr without overhead
> (control block alloc).
>
> Just using shared_ptr as much as possible avoids the issue but might not
> always be possible.
I don't find the example compelling enough to standardize a conversion from
any shared_ptr to unique_ptr. It would give a false impression what
the responsibility
of the pointee would be.
The current design already allows anyone to implement the request very easily,
like so:
#include <memory>
template<class T>
struct pseudo_deleter {
typedef std::shared_ptr<T> pointer;
void operator()(pointer) const {}
};
template<class T>
using funny_ptr = std::unique_ptr<T, pseudo_deleter<T>>;
int main()
{
std::shared_ptr<int> pi(new int(12));
funny_ptr<int> pf(pi);
}
I don't think that this is worth standardizing this: The request would have the
effect that code that was unintentionally written would compile and would
possible do different things as expected. This is different, if a user
explicitly
provides std::unique_ptr<T, pseudo_deleter<T>> to those who are interested
in such conversion (Add to the list a factory function template, if
you prefer). If this
conversion support result in funny programs they can complain to the inventor
of funny_ptr.
- Daniel