variant_copy_construct_cx.cpp 2.2 KB

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