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