variant_emplace_type_cx.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 explicit 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 explicit Y( int v ): v( v ) {}
  21. constexpr operator int() const { return v; }
  22. };
  23. #define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
  24. template<class V, class T, class A> constexpr A test( A const& a )
  25. {
  26. V v;
  27. v.template emplace<T>( a );
  28. return get<T>(v);
  29. }
  30. int main()
  31. {
  32. {
  33. constexpr auto w = test<variant<int>, int>( 1 );
  34. STATIC_ASSERT( w == 1 );
  35. }
  36. {
  37. constexpr auto w = test<variant<X>, X>( 1 );
  38. STATIC_ASSERT( w == 1 );
  39. }
  40. #if defined( BOOST_LIBSTDCXX_VERSION ) && BOOST_LIBSTDCXX_VERSION < 50000
  41. #else
  42. {
  43. constexpr auto w = test<variant<Y>, Y>( 1 );
  44. STATIC_ASSERT( w == 1 );
  45. }
  46. #endif
  47. {
  48. constexpr auto w = test<variant<int, float>, int>( 1 );
  49. STATIC_ASSERT( w == 1 );
  50. }
  51. {
  52. constexpr auto w = test<variant<int, float>, float>( 3.0f );
  53. STATIC_ASSERT( w == 3.0f );
  54. }
  55. {
  56. constexpr auto w = test<variant<int, int, float>, float>( 3.0f );
  57. STATIC_ASSERT( w == 3.0f );
  58. }
  59. {
  60. constexpr auto w = test<variant<int, int, float, float, X>, X>( 1 );
  61. STATIC_ASSERT( w == 1 );
  62. }
  63. #if defined( BOOST_LIBSTDCXX_VERSION ) && BOOST_LIBSTDCXX_VERSION < 50000
  64. #else
  65. {
  66. constexpr auto w = test<variant<int, int, float, float, Y>, Y>( 1 );
  67. STATIC_ASSERT( w == 1 );
  68. }
  69. #endif
  70. }