variant_in_place_type_construct_cx.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright 2017 Peter Dimov.
  2. //
  3. // Distributed under the Boost Software License, Version 1.0.
  4. //
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. #include <boost/variant2/variant.hpp>
  8. using namespace boost::variant2;
  9. struct X
  10. {
  11. constexpr X() = default;
  12. constexpr explicit X(int, int) {}
  13. template<class T> X( in_place_type_t<T> ) = delete;
  14. };
  15. #define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
  16. int main()
  17. {
  18. {
  19. constexpr variant<int> v( in_place_type_t<int>{} );
  20. STATIC_ASSERT( v.index() == 0 );
  21. STATIC_ASSERT( get<0>(v) == 0 );
  22. STATIC_ASSERT( holds_alternative<int>(v) );
  23. }
  24. {
  25. constexpr variant<X> v( in_place_type_t<X>{} );
  26. STATIC_ASSERT( v.index() == 0 );
  27. STATIC_ASSERT( holds_alternative<X>(v) );
  28. }
  29. {
  30. constexpr variant<int> v( in_place_type_t<int>{}, 1 );
  31. STATIC_ASSERT( v.index() == 0 );
  32. STATIC_ASSERT( get<0>(v) == 1 );
  33. STATIC_ASSERT( holds_alternative<int>(v) );
  34. }
  35. {
  36. constexpr variant<int, float> v( in_place_type_t<int>{} );
  37. STATIC_ASSERT( v.index() == 0 );
  38. STATIC_ASSERT( get<0>(v) == 0 );
  39. STATIC_ASSERT( holds_alternative<int>(v) );
  40. }
  41. {
  42. constexpr variant<int, float> v( in_place_type_t<int>{}, 1 );
  43. STATIC_ASSERT( v.index() == 0 );
  44. STATIC_ASSERT( get<0>(v) == 1 );
  45. STATIC_ASSERT( holds_alternative<int>(v) );
  46. }
  47. {
  48. constexpr variant<int, float> v( in_place_type_t<float>{} );
  49. STATIC_ASSERT( v.index() == 1 );
  50. STATIC_ASSERT( get<1>(v) == 0 );
  51. STATIC_ASSERT( holds_alternative<float>(v) );
  52. }
  53. {
  54. constexpr variant<int, float> v( in_place_type_t<float>{}, 3.14f );
  55. STATIC_ASSERT( v.index() == 1 );
  56. STATIC_ASSERT( get<1>(v) == 3.14f );
  57. STATIC_ASSERT( holds_alternative<float>(v) );
  58. }
  59. {
  60. constexpr variant<int, int, float, X> v( in_place_type_t<float>{}, 3.14f );
  61. STATIC_ASSERT( v.index() == 2 );
  62. STATIC_ASSERT( get<2>(v) == 3.14f );
  63. STATIC_ASSERT( holds_alternative<float>(v) );
  64. }
  65. {
  66. constexpr variant<int, int, float, float, X> v( in_place_type_t<X>{} );
  67. STATIC_ASSERT( v.index() == 4 );
  68. STATIC_ASSERT( holds_alternative<X>(v) );
  69. }
  70. {
  71. constexpr variant<int, int, float, float, X> v( in_place_type_t<X>{}, 0, 0 );
  72. STATIC_ASSERT( v.index() == 4 );
  73. STATIC_ASSERT( holds_alternative<X>(v) );
  74. }
  75. }