Home | History | Annotate | Download | only in variant.get
      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 <size_t I, class... Types>
     16 //  constexpr add_pointer_t<variant_alternative_t<I, variant<Types...>>>
     17 //   get_if(variant<Types...>* v) noexcept;
     18 // template <size_t I, class... Types>
     19 //  constexpr add_pointer_t<const variant_alternative_t<I, variant<Types...>>>
     20 //   get_if(const variant<Types...>* v) noexcept;
     21 
     22 #include "test_macros.h"
     23 #include "variant_test_helpers.hpp"
     24 #include <cassert>
     25 #include <memory>
     26 #include <variant>
     27 
     28 void test_const_get_if() {
     29   {
     30     using V = std::variant<int>;
     31     constexpr const V *v = nullptr;
     32     static_assert(std::get_if<0>(v) == nullptr, "");
     33   }
     34   {
     35     using V = std::variant<int, const long>;
     36     constexpr V v(42);
     37     ASSERT_NOEXCEPT(std::get_if<0>(&v));
     38     ASSERT_SAME_TYPE(decltype(std::get_if<0>(&v)), const int *);
     39     static_assert(*std::get_if<0>(&v) == 42, "");
     40     static_assert(std::get_if<1>(&v) == nullptr, "");
     41   }
     42   {
     43     using V = std::variant<int, const long>;
     44     constexpr V v(42l);
     45     ASSERT_SAME_TYPE(decltype(std::get_if<1>(&v)), const long *);
     46     static_assert(*std::get_if<1>(&v) == 42, "");
     47     static_assert(std::get_if<0>(&v) == nullptr, "");
     48   }
     49 // FIXME: Remove these once reference support is reinstated
     50 #if !defined(TEST_VARIANT_HAS_NO_REFERENCES)
     51   {
     52     using V = std::variant<int &>;
     53     int x = 42;
     54     const V v(x);
     55     ASSERT_SAME_TYPE(decltype(std::get_if<0>(&v)), int *);
     56     assert(std::get_if<0>(&v) == &x);
     57   }
     58   {
     59     using V = std::variant<int &&>;
     60     int x = 42;
     61     const V v(std::move(x));
     62     ASSERT_SAME_TYPE(decltype(std::get_if<0>(&v)), int *);
     63     assert(std::get_if<0>(&v) == &x);
     64   }
     65   {
     66     using V = std::variant<const int &&>;
     67     int x = 42;
     68     const V v(std::move(x));
     69     ASSERT_SAME_TYPE(decltype(std::get_if<0>(&v)), const int *);
     70     assert(std::get_if<0>(&v) == &x);
     71   }
     72 #endif
     73 }
     74 
     75 void test_get_if() {
     76   {
     77     using V = std::variant<int>;
     78     V *v = nullptr;
     79     assert(std::get_if<0>(v) == nullptr);
     80   }
     81   {
     82     using V = std::variant<int, long>;
     83     V v(42);
     84     ASSERT_NOEXCEPT(std::get_if<0>(&v));
     85     ASSERT_SAME_TYPE(decltype(std::get_if<0>(&v)), int *);
     86     assert(*std::get_if<0>(&v) == 42);
     87     assert(std::get_if<1>(&v) == nullptr);
     88   }
     89   {
     90     using V = std::variant<int, const long>;
     91     V v(42l);
     92     ASSERT_SAME_TYPE(decltype(std::get_if<1>(&v)), const long *);
     93     assert(*std::get_if<1>(&v) == 42);
     94     assert(std::get_if<0>(&v) == nullptr);
     95   }
     96 // FIXME: Remove these once reference support is reinstated
     97 #if !defined(TEST_VARIANT_HAS_NO_REFERENCES)
     98   {
     99     using V = std::variant<int &>;
    100     int x = 42;
    101     V v(x);
    102     ASSERT_SAME_TYPE(decltype(std::get_if<0>(&v)), int *);
    103     assert(std::get_if<0>(&v) == &x);
    104   }
    105   {
    106     using V = std::variant<const int &>;
    107     int x = 42;
    108     V v(x);
    109     ASSERT_SAME_TYPE(decltype(std::get_if<0>(&v)), const int *);
    110     assert(std::get_if<0>(&v) == &x);
    111   }
    112   {
    113     using V = std::variant<int &&>;
    114     int x = 42;
    115     V v(std::move(x));
    116     ASSERT_SAME_TYPE(decltype(std::get_if<0>(&v)), int *);
    117     assert(std::get_if<0>(&v) == &x);
    118   }
    119   {
    120     using V = std::variant<const int &&>;
    121     int x = 42;
    122     V v(std::move(x));
    123     ASSERT_SAME_TYPE(decltype(std::get_if<0>(&v)), const int *);
    124     assert(std::get_if<0>(&v) == &x);
    125   }
    126 #endif
    127 }
    128 
    129 int main() {
    130   test_const_get_if();
    131   test_get_if();
    132 }
    133