variant_move_assign_cx.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 <utility>
  9. using namespace boost::variant2;
  10. struct X
  11. {
  12. int v;
  13. X() = default;
  14. constexpr X( int v ): v( v ) {}
  15. constexpr operator int() const { return v; }
  16. };
  17. struct Y
  18. {
  19. int v;
  20. constexpr Y(): v() {}
  21. constexpr Y( int v ): v( v ) {}
  22. constexpr operator int() const { return v; }
  23. };
  24. enum E
  25. {
  26. v
  27. };
  28. #define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
  29. template<class V, class T, class A> constexpr T test( A&& a )
  30. {
  31. V v;
  32. v = std::forward<A>(a);
  33. return get<T>(v);
  34. }
  35. int main()
  36. {
  37. {
  38. constexpr auto w = test<variant<int>, int>( variant<int>( 1 ) );
  39. STATIC_ASSERT( w == 1 );
  40. }
  41. {
  42. constexpr auto w = test<variant<X>, X>( variant<X>( 1 ) );
  43. STATIC_ASSERT( w == 1 );
  44. }
  45. #if defined( BOOST_LIBSTDCXX_VERSION ) && BOOST_LIBSTDCXX_VERSION < 50000
  46. #else
  47. {
  48. constexpr auto w = test<variant<Y>, Y>( variant<Y>( 1 ) );
  49. STATIC_ASSERT( w == 1 );
  50. }
  51. #endif
  52. {
  53. constexpr auto w = test<variant<int, float>, int>( variant<int, float>( 1 ) );
  54. STATIC_ASSERT( w == 1 );
  55. }
  56. {
  57. constexpr auto w = test<variant<int, float>, float>( variant<int, float>( 3.0f ) );
  58. STATIC_ASSERT( w == 3.0f );
  59. }
  60. {
  61. constexpr auto w = test<variant<int, int, float>, float>( variant<int, int, float>( 3.0f ) );
  62. STATIC_ASSERT( w == 3.0f );
  63. }
  64. {
  65. constexpr auto w = test<variant<E, E, X>, X>( variant<E, E, X>( 1 ) );
  66. STATIC_ASSERT( w == 1 );
  67. }
  68. {
  69. constexpr auto w = test<variant<int, int, float, float, X>, X>( variant<int, int, float, float, X>( X(1) ) );
  70. STATIC_ASSERT( w == 1 );
  71. }
  72. #if defined( BOOST_LIBSTDCXX_VERSION ) && BOOST_LIBSTDCXX_VERSION < 50000
  73. #else
  74. {
  75. constexpr auto w = test<variant<E, E, Y>, Y>( variant<E, E, Y>( 1 ) );
  76. STATIC_ASSERT( w == 1 );
  77. }
  78. {
  79. constexpr auto w = test<variant<int, int, float, float, Y>, Y>( variant<int, int, float, float, Y>( Y(1) ) );
  80. STATIC_ASSERT( w == 1 );
  81. }
  82. #endif
  83. }