GMa*_*ckG 11 c++ smart-pointers std rationale c++11
在C++ 0x(n3126)中,可以比较智能指针,无论是关系还是相等.但是,这样做的方式似乎与我不一致.
例如,shared_ptr定义operator<相当于:
template <typename T, typename U>
bool operator<(const shared_ptr<T>& a, const shared_ptr<T>& b)
{
return std::less<void*>()(a.get(), b.get());
}
Run Code Online (Sandbox Code Playgroud)
std::less与未指定的vanilla关系指针比较不同,使用提供关于指针值的总排序.
但是,unique_ptr定义相同的运算符:
template <typename T1, typename D1, typename T2, typename D2>
bool operator<(const unique_ptr<T1, D1>& a, const unique_ptr<T2, D2>& b)
{
return a.get() < b.get();
}
Run Code Online (Sandbox Code Playgroud)
它还以类似的方式定义了其他关系运算符.
为什么改变方法和"完整性"?也就是说,为什么shared_ptr使用std::less,而unique_ptr使用内置的operator<?为什么不shared_ptr提供其他关系运算符,比如unique_ptr?
我可以理解这两种选择背后的理由:
std::less谓词模板参数获得)但我不明白为什么选择会根据智能指针类型而改变.我错过了什么?
奖金/相关:std::shared_ptr似乎已经跟随boost::shared_ptr,后者省略了"按设计"的其他关系运算符(也是如此std::shared_ptr).为什么是这样?
| 归档时间: |
|
| 查看次数: |
1177 次 |
| 最近记录: |