forked from TartanLlama/optional
Support std::vector of non-copyable types
This commit is contained in:
@ -1,5 +1,12 @@
|
||||
#include "catch.hpp"
|
||||
#include "optional.hpp"
|
||||
#include <vector>
|
||||
|
||||
struct foo {
|
||||
foo() = default;
|
||||
foo(foo&) = delete;
|
||||
foo(foo&&) {};
|
||||
};
|
||||
|
||||
TEST_CASE("Constructors", "[constructors]") {
|
||||
tl::optional<int> o1;
|
||||
@ -47,4 +54,9 @@ TEST_CASE("Constructors", "[constructors]") {
|
||||
REQUIRE(oo);
|
||||
REQUIRE(*oo == 42);
|
||||
}
|
||||
|
||||
std::vector<foo> v;
|
||||
v.emplace_back();
|
||||
tl::optional<std::vector<foo>> ov = std::move(v);
|
||||
REQUIRE(ov->size() == 1);
|
||||
}
|
||||
|
@ -55,6 +55,26 @@
|
||||
|
||||
// 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
|
||||
|
||||
// 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
|
||||
#define TL_OPTIONAL_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(T) \
|
||||
std::is_trivially_copy_constructible<T>::value
|
||||
|
Reference in New Issue
Block a user