Topic: Smart pointer comparison - why a difference?
Author: Bo Persson<bop@gmb.dk>
Date: Mon, 28 May 2012 14:53:04 -0700 (PDT)
Raw View
In C++11, operator< for unique_ptr [unique.ptr.special] is defined as
std::less<CT>()(x.get(), y.get())
where CT is the common_type of the unique_ptr's internal pointer typedefs.
On the other hand, operator< for shared_ptr [util.smartptr.shared.cmp]
is defined as
std::less<V>()(a.get(), b.get())
"where V is the composite pointer type (5.9) of T* and U*."
What exactly is the difference, and why?
Bo Persson
--
[ comp.std.c++ is moderated. To submit articles, try posting with your ]
[ newsreader. If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: =?ISO-8859-1?Q?Daniel_Kr=FCgler?=<daniel.kruegler@googlemail.com>
Date: Tue, 29 May 2012 11:25:39 -0700 (PDT)
Raw View
On 2012-05-28 23:53, Bo Persson wrote:
> In C++11, operator< for unique_ptr [unique.ptr.special] is defined as
>
> std::less<CT>()(x.get(), y.get())
>
> where CT is the common_type of the unique_ptr's internal pointer typedefs.
>
>
> On the other hand, operator< for shared_ptr [util.smartptr.shared.cmp]
> is defined as
>
> std::less<V>()(a.get(), b.get())
>
> "where V is the composite pointer type (5.9) of T* and U*."
>
>
> What exactly is the difference, and why?
The wording difference exists, because std::unique_ptr can hold both
built-in pointers and pointer-like types while std::shared_ptr can only
hold pointers. The intention is that unique_ptr's wording has exactly
the same effect when it holds a pointer. Currently these definitions are
not the same because of a wording defect of std::common_type. An issue
has already been raised for this but is not yet visible in the official
list. In short words, that defect is because std::common_type<int*,
int*>::type evaluates to int*&& and not int*. This was an unintended
side-effect because of change of the original definition of the
conditional operator in the presence of xvalues.
HTH& Greetings from Bremen,
Daniel Kr gler
--
[ comp.std.c++ is moderated. To submit articles, try posting with your ]
[ newsreader. If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]