x3_variant.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*=============================================================================
  2. Copyright (c) 2001-2016 Joel de Guzman
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. =============================================================================*/
  6. #include <boost/detail/lightweight_test.hpp>
  7. #include <boost/spirit/home/x3.hpp>
  8. #include <boost/spirit/home/x3/support/ast/variant.hpp>
  9. #include <string>
  10. #include <iostream>
  11. #include "test.hpp"
  12. namespace x3 = boost::spirit::x3;
  13. struct none {};
  14. using variant = x3::variant<
  15. none
  16. , bool
  17. , std::string
  18. , int
  19. , double
  20. >;
  21. struct ast : variant
  22. {
  23. using variant::variant;
  24. using variant::operator=;
  25. ast(char const* s)
  26. : variant(std::string{s})
  27. {}
  28. ast& operator=(char const* s)
  29. {
  30. variant::operator=(std::string{s});
  31. return *this;
  32. }
  33. };
  34. int
  35. main()
  36. {
  37. {
  38. ast v{123};
  39. BOOST_TEST(boost::get<int>(v) == 123);
  40. v = "test";
  41. BOOST_TEST(boost::get<std::string>(v) == "test");
  42. v = true;
  43. BOOST_TEST(boost::get<bool>(v) == true);
  44. }
  45. return boost::report_errors();
  46. }