variant_move_construct_cx.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 T, class V> constexpr T test( V&& v )
  30. {
  31. V v2( std::forward<V>(v) );
  32. return get<T>( v2 );
  33. }
  34. int main()
  35. {
  36. {
  37. constexpr auto w = test<int>( variant<int>( 1 ) );
  38. STATIC_ASSERT( w == 1 );
  39. }
  40. {
  41. constexpr auto w = test<X>( variant<X>( 1 ) );
  42. STATIC_ASSERT( w == 1 );
  43. }
  44. #if defined( BOOST_LIBSTDCXX_VERSION ) && BOOST_LIBSTDCXX_VERSION < 50000
  45. #else
  46. {
  47. constexpr auto w = test<Y>( variant<Y>( 1 ) );
  48. STATIC_ASSERT( w == 1 );
  49. }
  50. #endif
  51. {
  52. constexpr auto w = test<int>( variant<int, float>( 1 ) );
  53. STATIC_ASSERT( w == 1 );
  54. }
  55. {
  56. constexpr auto w = test<float>( variant<int, float>( 3.0f ) );
  57. STATIC_ASSERT( w == 3.0f );
  58. }
  59. {
  60. constexpr auto w = test<float>( variant<int, int, float>( 3.0f ) );
  61. STATIC_ASSERT( w == 3.0f );
  62. }
  63. {
  64. constexpr auto w = test<X>( variant<E, E, X>( 1 ) );
  65. STATIC_ASSERT( w == 1 );
  66. }
  67. {
  68. constexpr auto w = test<X>( variant<int, int, float, float, X>( X(1) ) );
  69. STATIC_ASSERT( w == 1 );
  70. }
  71. #if defined( BOOST_LIBSTDCXX_VERSION ) && BOOST_LIBSTDCXX_VERSION < 50000
  72. #else
  73. {
  74. constexpr auto w = test<Y>( variant<E, E, Y>( 1 ) );
  75. STATIC_ASSERT( w == 1 );
  76. }
  77. {
  78. constexpr auto w = test<Y>( variant<int, int, float, float, Y>( Y(1) ) );
  79. STATIC_ASSERT( w == 1 );
  80. }
  81. #endif
  82. }