应该`unique_ptr< T const []&`接受一个`T *`构造函数参数? [英] Should `unique_ptr< T const [] >` accept a `T*` constructor argument?
问题描述
代码:
#include <memory>
using namespace std;
struct T {};
T* foo() { return new T; }
T const* bar() { return foo(); }
int main()
{
unique_ptr< T const > p1( bar() ); // OK
unique_ptr< T const [] > a1( bar() ); // OK
unique_ptr< T const > p2( foo() ); // OK
unique_ptr< T const [] > a2( foo() ); // ? this is line #15
}
使用Visual C ++ 10.0和MinGW g ++的示例错误4.4。 1:
Example errors with Visual C++ 10.0 and MinGW g++ 4.4.1:
[d:\dev\test]
> cl foo.cpp
foo.cpp
foo.cpp(15) : error C2248: 'std::unique_ptr<_Ty>::unique_ptr' : cannot access private member declared in class 'std::unique_ptr<_Ty>'
with
[
_Ty=const T []
]
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\memory(2509) : see declaration of 'std::unique_ptr<_Ty>::unique_ptr'
with
[
_Ty=const T []
]
[d:\dev\test]
> g++ foo.cpp -std=c++0x
c:\program files (x86)\codeblocks\mingw\bin\../lib/gcc/mingw32/4.4.1/include/c++/bits/unique_ptr.h: In function 'int main()':
c:\program files (x86)\codeblocks\mingw\bin\../lib/gcc/mingw32/4.4.1/include/c++/bits/unique_ptr.h:379: error: deleted function 'std::unique_ptr<_Tp [], _Tp_Deleter>::unique_ptr(_Up*, typename std::enable_if<std::is_convertible::value, void>::type*) [with _Up = T, _Tp = const T, _Tp_Deleter = std::default_delete<const T []>]'
foo.cpp:15: error: used here
[d:\dev\test]
> _
在我看来,数组版本应该接受与非数组版本相同的隐式const添加。
It seems to me that the array version should accept the same implicit const-adding as the non-array version.
不同的是数组版本不应该接受一个派生类的指针,这是显然在上面的机制。
The difference is that the array version should not accept pointer to a derived class, and that's the machinery that apparently kicks in above.
如果代码正式无效,标准的措辞是否反映意图(即,是否适用于DR)?
If the code is formally invalid, does the standard's wording reflect the intent (i.e., is a DR appropriate)?
如果第一个没有,第二个是yes,则意味着有缺陷(即,再次是DR是否适当)?
If no to the first and yes to the second, is the intent defective (i.e., again, is a DR appropriate)?
推荐答案
缺陷报告可能是适当的。 §20.7.1.3.1说,
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;
这些构造函数的行为与主模板中的相同,不接受可转换为指针的指针类型。 [注意:一种实现技术是创建这些成员的专用模板重载。 - end note]
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 ]
这个想法显然是为了防止不适用于数组的派生到基础转换。但它是非特定的,cv-qualification转换也被禁止。也许应该更改为禁止指针转换(§4.10),而不是所有的指针转换。
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.
这篇关于应该`unique_ptr< T const []&`接受一个`T *`构造函数参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!