mirror of
https://github.com/boostorg/optional.git
synced 2025-07-16 22:02:07 +02:00
fixed defaulted moves on older compilers
This commit is contained in:
@ -19,7 +19,7 @@ Lifetime of the `T` inside `_storage` is manually controlled with placement-`new
|
|||||||
T _storage;
|
T _storage;
|
||||||
};
|
};
|
||||||
|
|
||||||
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`:
|
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 (including defaulted move assignment and constructor). 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
|
struct X // not trivial
|
||||||
{
|
{
|
||||||
|
@ -114,14 +114,16 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
// Detection of correctly implemented defaulted functions
|
// Detect suport for defaulting move operations
|
||||||
|
// (some older compilers implement rvalue references,
|
||||||
|
// defaulted funcitons but move operations are not special members and cannot be defaulted)
|
||||||
|
|
||||||
#ifdef BOOST_NO_CXX11_DEFAULTED_FUNCTIONS
|
#ifdef BOOST_NO_CXX11_DEFAULTED_FUNCTIONS
|
||||||
# define BOOST_OPTIONAL_DETAIL_NO_DEFAULTED_FUNCTIONS
|
# define BOOST_OPTIONAL_DETAIL_NO_DEFAULTED_MOVE_FUNCTIONS
|
||||||
#elif BOOST_WORKAROUND(BOOST_MSVC, < 1900)
|
#elif BOOST_WORKAROUND(BOOST_MSVC, < 1900)
|
||||||
// on MSVC 12.0 move constructor and move assignment are not reconized as special functions
|
# define BOOST_OPTIONAL_DETAIL_NO_DEFAULTED_MOVE_FUNCTIONS
|
||||||
// and they cannot be defaulted
|
#elif BOOST_WORKAROUND(BOOST_GCC_VERSION, < 40600)
|
||||||
# define BOOST_OPTIONAL_DETAIL_NO_DEFAULTED_FUNCTIONS
|
# define BOOST_OPTIONAL_DETAIL_NO_DEFAULTED_MOVE_FUNCTIONS
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
@ -20,7 +20,7 @@
|
|||||||
#include "boost/type_traits/is_base_of.hpp"
|
#include "boost/type_traits/is_base_of.hpp"
|
||||||
#include "boost/optional/detail/experimental_traits.hpp"
|
#include "boost/optional/detail/experimental_traits.hpp"
|
||||||
|
|
||||||
#ifndef BOOST_OPTIONAL_DETAIL_NO_DEFAULTED_FUNCTIONS
|
#ifndef BOOST_OPTIONAL_DETAIL_NO_DEFAULTED_MOVE_FUNCTIONS
|
||||||
|
|
||||||
struct PrivDefault
|
struct PrivDefault
|
||||||
{
|
{
|
||||||
@ -144,7 +144,7 @@ void test_trivial_copyability()
|
|||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
#ifndef BOOST_OPTIONAL_DETAIL_NO_DEFAULTED_FUNCTIONS
|
#ifndef BOOST_OPTIONAL_DETAIL_NO_DEFAULTED_MOVE_FUNCTIONS
|
||||||
test_type_traits();
|
test_type_traits();
|
||||||
test_trivial_copyability();
|
test_trivial_copyability();
|
||||||
#endif
|
#endif
|
||||||
|
@ -20,7 +20,7 @@
|
|||||||
#include "boost/type_traits/is_base_of.hpp"
|
#include "boost/type_traits/is_base_of.hpp"
|
||||||
#include "boost/optional/detail/experimental_traits.hpp"
|
#include "boost/optional/detail/experimental_traits.hpp"
|
||||||
|
|
||||||
#ifndef BOOST_OPTIONAL_DETAIL_NO_DEFAULTED_FUNCTIONS
|
#ifndef BOOST_OPTIONAL_DETAIL_NO_DEFAULTED_MOVE_FUNCTIONS
|
||||||
|
|
||||||
struct PrivDefault
|
struct PrivDefault
|
||||||
{
|
{
|
||||||
@ -138,7 +138,7 @@ void test_trivial_copyability()
|
|||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
#ifndef BOOST_OPTIONAL_DETAIL_NO_DEFAULTED_FUNCTIONS
|
#ifndef BOOST_OPTIONAL_DETAIL_NO_DEFAULTED_MOVE_FUNCTIONS
|
||||||
test_type_traits();
|
test_type_traits();
|
||||||
test_trivial_copyability();
|
test_trivial_copyability();
|
||||||
#endif
|
#endif
|
||||||
|
Reference in New Issue
Block a user