1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <optional>
// template <class U> optional<T>& operator=(U&& v);
#include <experimental/optional>
#include <type_traits>
#include <cassert>
#include <memory>
#if _LIBCPP_STD_VER > 11
using std::experimental::optional;
struct X
{
};
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
static_assert(std::is_assignable<optional<int>, int>::value, "");
static_assert(std::is_assignable<optional<int>, int&>::value, "");
static_assert(std::is_assignable<optional<int>&, int>::value, "");
static_assert(std::is_assignable<optional<int>&, int&>::value, "");
static_assert(std::is_assignable<optional<int>&, const int&>::value, "");
static_assert(!std::is_assignable<const optional<int>&, const int&>::value, "");
static_assert(!std::is_assignable<optional<int>, X>::value, "");
{
optional<int> opt;
opt = 1;
assert(static_cast<bool>(opt) == true);
assert(*opt == 1);
}
{
optional<int> opt;
const int i = 2;
opt = i;
assert(static_cast<bool>(opt) == true);
assert(*opt == i);
}
{
optional<int> opt(3);
const int i = 2;
opt = i;
assert(static_cast<bool>(opt) == true);
assert(*opt == i);
}
{
optional<std::unique_ptr<int>> opt;
opt = std::unique_ptr<int>(new int(3));
assert(static_cast<bool>(opt) == true);
assert(**opt == 3);
}
{
optional<std::unique_ptr<int>> opt(std::unique_ptr<int>(new int(2)));
opt = std::unique_ptr<int>(new int(3));
assert(static_cast<bool>(opt) == true);
assert(**opt == 3);
}
#endif // _LIBCPP_STD_VER > 11
}
|