Should `unique_ptr< T>` accept a `T*` constructor argument?

后端 未结 1 1713
长发绾君心
长发绾君心 2021-02-19 13:11

Code:

#include 
using namespace std;

struct T {};

T* foo() { return new T; }
T const* bar() { return foo(); }

int main()
{
    unique_ptr< T          


        
1条回答
  •  日久生厌
    2021-02-19 13:44

    A defect report may be appropriate. §20.7.1.3.1 says,

    explicit unique_ptr(pointer p) noexcept;
    unique_ptr(pointer p, see below d) noexcept;
    unique_ptr(pointer p, see below d) noexcept;
    

    These constructors behave the same as in the primary template except that they do not accept pointer types which are convertible to pointer. [Note: One implementation technique is to create private templated overloads of these members. — end note ]

    The idea is clearly to prevent derived-to-base conversions that don't work with arrays. But it is unspecific and cv-qualification conversion is forbidden too. Perhaps it should be changed to forbid pointer conversions (§4.10), not all conversions of pointers.

    0 讨论(0)
提交回复
热议问题