Home | History | Annotate | Download | only in optional.specalg
      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, c++14
     11 // <optional>
     12 
     13 // template <class T, class U, class... Args>
     14 //   constexpr optional<T> make_optional(initializer_list<U> il, Args&&... args);
     15 
     16 #include <optional>
     17 #include <string>
     18 #include <memory>
     19 #include <cassert>
     20 
     21 #include "test_macros.h"
     22 
     23 struct TestT {
     24   int x;
     25   int size;
     26   constexpr TestT(std::initializer_list<int> il) : x(*il.begin()), size(static_cast<int>(il.size())) {}
     27   constexpr TestT(std::initializer_list<int> il, const int*)
     28       : x(*il.begin()), size(static_cast<int>(il.size())) {}
     29 };
     30 
     31 int main()
     32 {
     33     using std::make_optional;
     34     {
     35         constexpr auto opt = make_optional<TestT>({42, 2, 3});
     36         ASSERT_SAME_TYPE(decltype(opt), const std::optional<TestT>);
     37         static_assert(opt->x == 42, "");
     38         static_assert(opt->size == 3, "");
     39     }
     40     {
     41         constexpr auto opt = make_optional<TestT>({42, 2, 3}, nullptr);
     42         static_assert(opt->x == 42, "");
     43         static_assert(opt->size == 3, "");
     44     }
     45     {
     46         auto opt = make_optional<std::string>({'1', '2', '3'});
     47         assert(*opt == "123");
     48     }
     49     {
     50         auto opt = make_optional<std::string>({'a', 'b', 'c'}, std::allocator<char>{});
     51         assert(*opt == "abc");
     52     }
     53 }
     54