为什么没有std :: shared_ptr< T []>专业化?
标准提供了 std :: unique_ptr
的模板专用化,它正确地从其析构函数中调用 delete []
:
The standard provides a template specialization of std::unique_ptr
which correctly calls the delete[]
from its destructor:
void func()
{
std::unique_ptr< int[] > arr(new int[10]);
.......
}
std :: shared_ptr
此专业化不可用,因此有必要提供一个删除程序
正确调用 delete [] :
With std::shared_ptr
this specialisation is not available, so it is necessary to
to provide a deleter which correctly calls delete[]
:
void func()
{
// Usage
shared_ptr array (new double [256], [](double* arr) { delete [] arr; } );
..............
}
b $ b
这只是一个疏忽吗? (以相同的方式,有一个 std :: copy_if
)或有理由吗?
LWG(C ++委员会的图书馆工作组)简要地考虑了这种可能性,但这个想法并不是没有争议。虽然争论主要是关于可以被抛弃的 shared_ptr< T []>
提议中添加的特征(算术 shared_ptr< T [ ]>
)。
The LWG (Library Working Group of the C++ committee) briefly considered the possibility but the idea wasn't without controversy. Though the controversy was mainly about a feature added to the shared_ptr<T[]>
proposal that could have been jettisoned (arithmetic on shared_ptr<T[]>
).
但最终真正的原因是,虽然讨论过, LWG来做到这一点。
But ultimately the real real reason is that though it was discussed, there was never an actual written proposal in front of the LWG to do this. It never bubbled up anyone's priority list (including my own) sufficiently to put the time into writing a proposal.
非正式对话最近在几个LWG中重新开始了这个主题成员,我个人原型。但是还没有书面建议。我认为这将是一个体面的工具箱中的额外工具。
Informal conversations have recently begun anew on this topic among a few LWG members, and I have personally prototyped it. But there is still no written proposal for it. I think it would be a decent additional tool in the toolbox. Whether it will ever actually happen or not is anyone's guess.
更新:
对 shared_ptr
的数组支持现在有一个草稿TS:
Array support for shared_ptr
now has a draft TS:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4077.html