cxx/test/utilities/optional/optional.object/optional.object.assign/assign_value.pass.cpp
Howard Hinnant 01afa5c6e4 Implement N3672, optional<T>.
git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@189772 91177308-0d34-0410-b5e6-96231b3b80d8
2013-09-02 20:30:37 +00:00

71 lines
2.0 KiB
C++

//===----------------------------------------------------------------------===//
//
// 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 <optional>
#include <type_traits>
#include <cassert>
#include <memory>
#if _LIBCPP_STD_VER > 11
struct X
{
};
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
static_assert(std::is_assignable<std::optional<int>, int>::value, "");
static_assert(std::is_assignable<std::optional<int>, int&>::value, "");
static_assert(std::is_assignable<std::optional<int>&, int>::value, "");
static_assert(std::is_assignable<std::optional<int>&, int&>::value, "");
static_assert(std::is_assignable<std::optional<int>&, const int&>::value, "");
static_assert(!std::is_assignable<const std::optional<int>&, const int&>::value, "");
static_assert(!std::is_assignable<std::optional<int>, X>::value, "");
{
std::optional<int> opt;
opt = 1;
assert(static_cast<bool>(opt) == true);
assert(*opt == 1);
}
{
std::optional<int> opt;
const int i = 2;
opt = i;
assert(static_cast<bool>(opt) == true);
assert(*opt == i);
}
{
std::optional<int> opt(3);
const int i = 2;
opt = i;
assert(static_cast<bool>(opt) == true);
assert(*opt == i);
}
{
std::optional<std::unique_ptr<int>> opt;
opt = std::unique_ptr<int>(new int(3));
assert(static_cast<bool>(opt) == true);
assert(**opt == 3);
}
{
std::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
}