【发布时间】:2016-05-10 23:38:17
【问题描述】:
目前(自 C++11 以来)使用 std::unique_ptr 设计 boost::recursive_wrapper 很简单:
template< typename T >
class recursive_wrapper
{
std::unique_ptr< T > storage;
public :
template< typename ...Args >
recursive_wrapper(Args &&... args)
: storage(std::make_unique< T >(std::forward< Args >(args)...))
{ ; }
template< typename R >
operator R & () noexcept
{
return static_cast< R & >(*storage);
}
template< typename R >
operator R const & () const noexcept
{
return static_cast< R const & >(*storage);
}
void
swap(recursive_wrapper & other) noexcept
{
storage.swap(other.storage);
}
};
但目前它是通过运算符::new 和boost::checked_delete 设计的。在现代 C++ 中使用原始 new 和 delete 运算符被认为是一种不好的做法。
如果目标只是 C++11 和更新版本,使用上面的 std::unique_ptr 实现 recursive_wrapper 是否有任何缺点(我的意思是编译时性能和运行时例如性能下降或其他)?如果作为要求的向后兼容性不再存在怎么办?
【问题讨论】:
标签: c++ c++11 boost smart-pointers boost-variant