variant_in_place_index_construct_cx.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. #include <boost/core/lightweight_test.hpp>
  9. #include <boost/core/lightweight_test_trait.hpp>
  10. #include <type_traits>
  11. #include <utility>
  12. #include <string>
  13. using namespace boost::variant2;
  14. struct X
  15. {
  16. constexpr X() = default;
  17. constexpr explicit X(int, int) {}
  18. X( in_place_index_t<0> ) = delete;
  19. };
  20. #define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
  21. int main()
  22. {
  23. {
  24. constexpr variant<int> v( in_place_index_t<0>{} );
  25. STATIC_ASSERT( v.index() == 0 );
  26. STATIC_ASSERT( get<0>(v) == 0 );
  27. }
  28. {
  29. constexpr variant<X> v( in_place_index_t<0>{} );
  30. STATIC_ASSERT( v.index() == 0 );
  31. }
  32. {
  33. constexpr variant<int> v( in_place_index_t<0>{}, 1 );
  34. STATIC_ASSERT( v.index() == 0 );
  35. STATIC_ASSERT( get<0>(v) == 1 );
  36. }
  37. {
  38. constexpr variant<int, float> v( in_place_index_t<0>{} );
  39. STATIC_ASSERT( v.index() == 0 );
  40. STATIC_ASSERT( get<0>(v) == 0 );
  41. }
  42. {
  43. constexpr variant<int, float> v( in_place_index_t<0>{}, 1 );
  44. STATIC_ASSERT( v.index() == 0 );
  45. STATIC_ASSERT( get<0>(v) == 1 );
  46. }
  47. {
  48. constexpr variant<int, float> v( in_place_index_t<1>{} );
  49. STATIC_ASSERT( v.index() == 1 );
  50. STATIC_ASSERT( get<1>(v) == 0 );
  51. }
  52. {
  53. constexpr variant<int, float> v( in_place_index_t<1>{}, 3.14f );
  54. STATIC_ASSERT( v.index() == 1 );
  55. STATIC_ASSERT( get<1>(v) == 3.14f );
  56. }
  57. {
  58. constexpr variant<int, int, float, float, X, X> v( in_place_index_t<0>{}, 1 );
  59. STATIC_ASSERT( v.index() == 0 );
  60. STATIC_ASSERT( get<0>(v) == 1 );
  61. }
  62. {
  63. constexpr variant<int, int, float, float, X, X> v( in_place_index_t<1>{}, 1 );
  64. STATIC_ASSERT( v.index() == 1 );
  65. STATIC_ASSERT( get<1>(v) == 1 );
  66. }
  67. {
  68. constexpr variant<int, int, float, float, X, X> v( in_place_index_t<2>{}, 3.14f );
  69. STATIC_ASSERT( v.index() == 2 );
  70. STATIC_ASSERT( get<2>(v) == 3.14f );
  71. }
  72. {
  73. constexpr variant<int, int, float, float, X, X> v( in_place_index_t<3>{}, 3.14f );
  74. STATIC_ASSERT( v.index() == 3 );
  75. STATIC_ASSERT( get<3>(v) == 3.14f );
  76. }
  77. {
  78. constexpr variant<int, int, float, float, X, X> v( in_place_index_t<4>{} );
  79. STATIC_ASSERT( v.index() == 4 );
  80. }
  81. {
  82. constexpr variant<int, int, float, float, X, X> v( in_place_index_t<5>{}, 0, 0 );
  83. STATIC_ASSERT( v.index() == 5 );
  84. }
  85. }