Home | History | Annotate | Download | only in variant.ctor
      1 // -*- C++ -*-
      2 //===----------------------------------------------------------------------===//
      3 //
      4 //                     The LLVM Compiler Infrastructure
      5 //
      6 // This file is dual licensed under the MIT and the University of Illinois Open
      7 // Source Licenses. See LICENSE.TXT for details.
      8 //
      9 //===----------------------------------------------------------------------===//
     10 
     11 // UNSUPPORTED: c++98, c++03, c++11, c++14
     12 
     13 // <variant>
     14 
     15 // template <class ...Types> class variant;
     16 
     17 // template <size_t I, class Up, class ...Args>
     18 // constexpr explicit
     19 // variant(in_place_index_t<I>, initializer_list<Up>, Args&&...);
     20 
     21 #include <cassert>
     22 #include <string>
     23 #include <type_traits>
     24 #include <variant>
     25 
     26 #include "test_convertible.hpp"
     27 #include "test_macros.h"
     28 
     29 struct InitList {
     30   std::size_t size;
     31   constexpr InitList(std::initializer_list<int> il) : size(il.size()) {}
     32 };
     33 
     34 struct InitListArg {
     35   std::size_t size;
     36   int value;
     37   constexpr InitListArg(std::initializer_list<int> il, int v)
     38       : size(il.size()), value(v) {}
     39 };
     40 
     41 void test_ctor_sfinae() {
     42   using IL = std::initializer_list<int>;
     43   { // just init list
     44     using V = std::variant<InitList, InitListArg, int>;
     45     static_assert(std::is_constructible<V, std::in_place_index_t<0>, IL>::value,
     46                   "");
     47     static_assert(!test_convertible<V, std::in_place_index_t<0>, IL>(), "");
     48   }
     49   { // too many arguments
     50     using V = std::variant<InitList, InitListArg, int>;
     51     static_assert(
     52         !std::is_constructible<V, std::in_place_index_t<0>, IL, int>::value,
     53         "");
     54     static_assert(!test_convertible<V, std::in_place_index_t<0>, IL, int>(),
     55                   "");
     56   }
     57   { // too few arguments
     58     using V = std::variant<InitList, InitListArg, int>;
     59     static_assert(
     60         !std::is_constructible<V, std::in_place_index_t<1>, IL>::value, "");
     61     static_assert(!test_convertible<V, std::in_place_index_t<1>, IL>(), "");
     62   }
     63   { // init list and arguments
     64     using V = std::variant<InitList, InitListArg, int>;
     65     static_assert(
     66         std::is_constructible<V, std::in_place_index_t<1>, IL, int>::value, "");
     67     static_assert(!test_convertible<V, std::in_place_index_t<1>, IL, int>(),
     68                   "");
     69   }
     70   { // not constructible from arguments
     71     using V = std::variant<InitList, InitListArg, int>;
     72     static_assert(
     73         !std::is_constructible<V, std::in_place_index_t<2>, IL>::value, "");
     74     static_assert(!test_convertible<V, std::in_place_index_t<2>, IL>(), "");
     75   }
     76 }
     77 
     78 void test_ctor_basic() {
     79   {
     80     constexpr std::variant<InitList, InitListArg, InitList> v(
     81         std::in_place_index<0>, {1, 2, 3});
     82     static_assert(v.index() == 0, "");
     83     static_assert(std::get<0>(v).size == 3, "");
     84   }
     85   {
     86     constexpr std::variant<InitList, InitListArg, InitList> v(
     87         std::in_place_index<2>, {1, 2, 3});
     88     static_assert(v.index() == 2, "");
     89     static_assert(std::get<2>(v).size == 3, "");
     90   }
     91   {
     92     constexpr std::variant<InitList, InitListArg, InitListArg> v(
     93         std::in_place_index<1>, {1, 2, 3, 4}, 42);
     94     static_assert(v.index() == 1, "");
     95     static_assert(std::get<1>(v).size == 4, "");
     96     static_assert(std::get<1>(v).value == 42, "");
     97   }
     98 }
     99 
    100 int main() {
    101   test_ctor_basic();
    102   test_ctor_sfinae();
    103 }
    104