variant_value_assign_cx.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 A test( A const& a )
  29. {
  30. V v;
  31. v = a;
  32. return get<T>(v);
  33. }
  34. int main()
  35. {
  36. {
  37. constexpr auto w = test<variant<int>, int>( 1 );
  38. STATIC_ASSERT( w == 1 );
  39. }
  40. {
  41. constexpr auto w = test<variant<X>, 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<variant<Y>, Y>( 1 );
  48. STATIC_ASSERT( w == 1 );
  49. }
  50. #endif
  51. {
  52. constexpr auto w = test<variant<int, float>, int>( 1 );
  53. STATIC_ASSERT( w == 1 );
  54. }
  55. {
  56. constexpr auto w = test<variant<int, float>, float>( 3.0f );
  57. STATIC_ASSERT( w == 3.0f );
  58. }
  59. {
  60. constexpr auto w = test<variant<int, int, float>, float>( 3.0f );
  61. STATIC_ASSERT( w == 3.0f );
  62. }
  63. {
  64. constexpr auto w = test<variant<E, E, X>, X>( 1 );
  65. STATIC_ASSERT( w == 1 );
  66. }
  67. {
  68. constexpr auto w = test<variant<int, int, float, float, X>, 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<variant<E, E, Y>, Y>( 1 );
  75. STATIC_ASSERT( w == 1 );
  76. }
  77. {
  78. constexpr auto w = test<variant<int, int, float, float, Y>, Y>( Y(1) );
  79. STATIC_ASSERT( w == 1 );
  80. }
  81. #endif
  82. }