Support std::vector of non-copyable types

This commit is contained in:
Simon Brand
2018-08-14 09:56:31 +01:00
parent 35e4c6e889
commit 9df8d1be39
2 changed files with 32 additions and 0 deletions

View File

@ -1,5 +1,12 @@
#include "catch.hpp" #include "catch.hpp"
#include "optional.hpp" #include "optional.hpp"
#include <vector>
struct foo {
foo() = default;
foo(foo&) = delete;
foo(foo&&) {};
};
TEST_CASE("Constructors", "[constructors]") { TEST_CASE("Constructors", "[constructors]") {
tl::optional<int> o1; tl::optional<int> o1;
@ -47,4 +54,9 @@ TEST_CASE("Constructors", "[constructors]") {
REQUIRE(oo); REQUIRE(oo);
REQUIRE(*oo == 42); REQUIRE(*oo == 42);
} }
std::vector<foo> v;
v.emplace_back();
tl::optional<std::vector<foo>> ov = std::move(v);
REQUIRE(ov->size() == 1);
} }

View File

@ -55,6 +55,26 @@
// This one will be different for GCC 5.7 if it's ever supported // This one will be different for GCC 5.7 if it's ever supported
#define TL_OPTIONAL_IS_TRIVIALLY_DESTRUCTIBLE(T) std::is_trivially_destructible<T>::value #define TL_OPTIONAL_IS_TRIVIALLY_DESTRUCTIBLE(T) std::is_trivially_destructible<T>::value
// GCC 5 < v < 8 has a bug in is_trivially_copy_constructible which breaks std::vector
// for non-copyable types
#elif (defined(__GNUC__) && __GNUC__ < 8 && \
!defined(__clang__))
namespace tl {
namespace detail {
template<class T>
struct is_trivially_copy_constructible : std::is_trivially_copy_constructible<T>{};
template<class T, class A>
struct is_trivially_copy_constructible<std::vector<T,A>>
: std::is_trivially_copy_constructible<T>{};
}
}
#define TL_OPTIONAL_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(T) \
tl::detail::is_trivially_copy_constructible<T>::value
#define TL_OPTIONAL_IS_TRIVIALLY_COPY_ASSIGNABLE(T) \
std::is_trivially_copy_assignable<T>::value
#define TL_OPTIONAL_IS_TRIVIALLY_DESTRUCTIBLE(T) std::is_trivially_destructible<T>::value
#else #else
#define TL_OPTIONAL_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(T) \ #define TL_OPTIONAL_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(T) \
std::is_trivially_copy_constructible<T>::value std::is_trivially_copy_constructible<T>::value