13
代碼:`unique_ptr <T const []>`接受一個`T *`構造函數參數嗎?
#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克++ 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] > _
在我看來該陣列版本應該接受相同的隱式常量-添加作爲非陣列版本。
不同之處在於數組版本不應接受指向派生類的指針,而這正是上面顯示的機器。
代碼是否有效?
如果代碼在形式上無效,標準的措辭是否反映了意圖(即,DR是否合適)?
如果不是第一個和是第二個,意向是否有缺陷(即同樣是適當的DR)?
」可轉換爲指針的指針類型「 - 並且真的挑剔,這不是語言平坦不正確嗎? `pointer`是一個可轉換爲指針的指針類型(例如,當描述概念時,如果某個函數返回一個「可轉換爲T」的類型,這並不意味着它不能是T本身)。但我不認爲其目的是除了`指針`以及派生類型的指針;-) – 2011-12-16 10:38:26