Currently (since C++11) it is simple to design boost::recursive_wrapper
using std::unique_ptr
:
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);
}
};
But currently it designed via operator ::new
and boost::checked_delete
. It is considered a bad practice to use raw new
and delete
operators in modern C++.
If target is only C++11 and newer, are there any downsides to implement recursive_wrapper
using std::unique_ptr
as it is above (I mean both compile-time performance and run-time performance degradation for example or maybe something else)? What if backward compatibility as a requirement ceased to exist?
Aucun commentaire:
Enregistrer un commentaire