variant_emplace_index_cx.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. int v;
  12. X() = default;
  13. constexpr explicit X( int v ): v( v ) {}
  14. constexpr operator int() const { return v; }
  15. };
  16. struct Y
  17. {
  18. int v;
  19. constexpr Y(): v() {}
  20. constexpr explicit Y( int v ): v( v ) {}
  21. constexpr operator int() const { return v; }
  22. };
  23. #define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
  24. template<class V, std::size_t I, class A> constexpr A test( A const& a )
  25. {
  26. V v;
  27. v.template emplace<I>( a );
  28. return get<I>(v);
  29. }
  30. int main()
  31. {
  32. {
  33. constexpr auto w = test<variant<int>, 0>( 1 );
  34. STATIC_ASSERT( w == 1 );
  35. }
  36. {
  37. constexpr auto w = test<variant<X>, 0>( 1 );
  38. STATIC_ASSERT( w == 1 );
  39. }
  40. #if defined( BOOST_LIBSTDCXX_VERSION ) && BOOST_LIBSTDCXX_VERSION < 50000
  41. #else
  42. {
  43. constexpr auto w = test<variant<Y>, 0>( 1 );
  44. STATIC_ASSERT( w == 1 );
  45. }
  46. #endif
  47. {
  48. constexpr auto w = test<variant<int, float>, 0>( 1 );
  49. STATIC_ASSERT( w == 1 );
  50. }
  51. {
  52. constexpr auto w = test<variant<int, float>, 1>( 3.0f );
  53. STATIC_ASSERT( w == 3.0f );
  54. }
  55. {
  56. constexpr auto w = test<variant<int, int, float, float, X, X>, 0>( 1 );
  57. STATIC_ASSERT( w == 1 );
  58. }
  59. {
  60. constexpr auto w = test<variant<int, int, float, float, X, X>, 1>( 1 );
  61. STATIC_ASSERT( w == 1 );
  62. }
  63. {
  64. constexpr auto w = test<variant<int, int, float, float, X, X>, 2>( 2.0f );
  65. STATIC_ASSERT( w == 2.0f );
  66. }
  67. {
  68. constexpr auto w = test<variant<int, int, float, float, X, X>, 3>( 3.0f );
  69. STATIC_ASSERT( w == 3.0f );
  70. }
  71. {
  72. constexpr auto w = test<variant<int, int, float, float, X, X>, 4>( 4 );
  73. STATIC_ASSERT( w == 4 );
  74. }
  75. {
  76. constexpr auto w = test<variant<int, int, float, float, X, X>, 5>( 5 );
  77. STATIC_ASSERT( w == 5 );
  78. }
  79. #if defined( BOOST_LIBSTDCXX_VERSION ) && BOOST_LIBSTDCXX_VERSION < 50000
  80. #else
  81. {
  82. constexpr auto w = test<variant<int, int, float, float, Y, Y>, 0>( 1 );
  83. STATIC_ASSERT( w == 1 );
  84. }
  85. {
  86. constexpr auto w = test<variant<int, int, float, float, Y, Y>, 1>( 1 );
  87. STATIC_ASSERT( w == 1 );
  88. }
  89. {
  90. constexpr auto w = test<variant<int, int, float, float, Y, Y>, 2>( 2.0f );
  91. STATIC_ASSERT( w == 2.0f );
  92. }
  93. {
  94. constexpr auto w = test<variant<int, int, float, float, Y, Y>, 3>( 3.0f );
  95. STATIC_ASSERT( w == 3.0f );
  96. }
  97. {
  98. constexpr auto w = test<variant<int, int, float, float, Y, Y>, 4>( 4 );
  99. STATIC_ASSERT( w == 4 );
  100. }
  101. {
  102. constexpr auto w = test<variant<int, int, float, float, Y, Y>, 5>( 5 );
  103. STATIC_ASSERT( w == 5 );
  104. }
  105. #endif
  106. }