variant_holds_alternative_cx.cpp 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. #define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
  10. int main()
  11. {
  12. {
  13. constexpr variant<int> v;
  14. STATIC_ASSERT( holds_alternative<int>( v ) );
  15. }
  16. {
  17. constexpr variant<int, float> v;
  18. STATIC_ASSERT( holds_alternative<int>( v ) );
  19. STATIC_ASSERT( !holds_alternative<float>( v ) );
  20. }
  21. {
  22. constexpr variant<int, float> v( 3.14f );
  23. STATIC_ASSERT( !holds_alternative<int>( v ) );
  24. STATIC_ASSERT( holds_alternative<float>( v ) );
  25. }
  26. {
  27. constexpr variant<int, float, float> v;
  28. STATIC_ASSERT( holds_alternative<int>( v ) );
  29. }
  30. {
  31. constexpr variant<int, int, float> v( 3.14f );
  32. STATIC_ASSERT( holds_alternative<float>( v ) );
  33. }
  34. }