building-argumentpacks0.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #include <boost/parameter.hpp>
  2. #include <iostream>
  3. BOOST_PARAMETER_NAME(index)
  4. template <typename ArgumentPack>
  5. int print_index(ArgumentPack const& args)
  6. {
  7. std::cout << "index = " << args[_index] << std::endl;
  8. return 0;
  9. }
  10. BOOST_PARAMETER_NAME(name)
  11. template <typename ArgumentPack>
  12. int print_name_and_index(ArgumentPack const& args)
  13. {
  14. std::cout << "name = " << args[_name] << "; ";
  15. return print_index(args);
  16. }
  17. #include <boost/core/lightweight_test.hpp>
  18. #include <boost/mpl/bool.hpp>
  19. #include <boost/mpl/placeholders.hpp>
  20. #include <boost/mpl/if.hpp>
  21. #include <boost/type_traits/is_convertible.hpp>
  22. int main()
  23. {
  24. int x = print_index(_index = 3); // prints "index = 3"
  25. int y = print_name_and_index((_index = 3, _name = "jones"));
  26. boost::parameter::parameters<
  27. boost::parameter::required<
  28. tag::name
  29. , boost::mpl::if_<
  30. boost::is_convertible<boost::mpl::_,char const*>
  31. , boost::mpl::true_
  32. , boost::mpl::false_
  33. >
  34. >
  35. , boost::parameter::optional<
  36. tag::index
  37. , boost::mpl::if_<
  38. boost::is_convertible<boost::mpl::_,int>
  39. , boost::mpl::true_
  40. , boost::mpl::false_
  41. >
  42. >
  43. > spec;
  44. char const sam[] = "sam";
  45. int twelve = 12;
  46. int z0 = print_name_and_index(spec(sam, twelve));
  47. int z1 = print_name_and_index(spec(_index=12, _name="sam"));
  48. BOOST_TEST(!x);
  49. BOOST_TEST(!y);
  50. BOOST_TEST(!z0);
  51. BOOST_TEST(!z1);
  52. return boost::report_errors();
  53. }