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