mirror of
https://github.com/boostorg/optional.git
synced 2025-08-01 05:24:32 +02:00
Reverted specialization for trivial types
It caused too many problems. I left only specialiation for scalar types. I will need to devise clever type traits for reconizing trivial types with working constructor.
This commit is contained in:
@@ -10,7 +10,7 @@ Technical details aside, the memory layout of `optional<T>` for a generic `T` is
|
||||
std::aligned_storage_t<sizeof(t), alignof(T)> _storage;
|
||||
};
|
||||
|
||||
Lifetime of the `T` inside `_storage` is manually controlled with placement-`new`s and pseudo-destructor calls. However, for trivial `T`s we use a different way of storage, by simply holding a `T`:
|
||||
Lifetime of the `T` inside `_storage` is manually controlled with placement-`new`s and pseudo-destructor calls. However, for scalar `T`s we use a different way of storage, by simply holding a `T`:
|
||||
|
||||
template <typename T>
|
||||
class optional
|
||||
@@ -19,7 +19,7 @@ Lifetime of the `T` inside `_storage` is manually controlled with placement-`new
|
||||
T _storage;
|
||||
};
|
||||
|
||||
We call it a ['direct] storage. This makes `optional<T>` a trivially-copyable type for trivial `T`s. This only works for compilers that support defaulted functions and type traits. On compilers without defaulted functions we still use the direct storage, but `optional<T>` is no longer recognized as trivially-copyable. On compilers that do not fully support type traits, we still use the direct storage for scalar types, but we leave the programmer a way of customizing her type, so that it is reconized by `optional` as trivial, by specializing type trait `boost::opitonal_config::is_type_trivial`:
|
||||
We call it a ['direct] storage. This makes `optional<T>` a trivially-copyable type for scalar `T`s. This only works for compilers that support defaulted functions. On compilers without defaulted functions we still use the direct storage, but `optional<T>` is no longer recognized as trivially-copyable. Apart from scalar types, we leave the programmer a way of customizing her type, so that it is reconized by `optional` as candidate for optimized storage, by specializing type trait `boost::opitonal_config::optional_uses_direct_storage_for`:
|
||||
|
||||
struct X // not trivial
|
||||
{
|
||||
@@ -28,7 +28,7 @@ We call it a ['direct] storage. This makes `optional<T>` a trivially-copyable ty
|
||||
|
||||
namespace boost { namespace optional_config {
|
||||
|
||||
template <> struct is_type_trivial<X> : boost::true_type {};
|
||||
template <> struct optional_uses_direct_storage_for<X> : boost::true_type {};
|
||||
|
||||
}}
|
||||
|
||||
|
Reference in New Issue
Block a user