macro_type_args_example.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // (C) Copyright Tobias Schwinger
  2. //
  3. // Use modification and distribution are subject to the boost Software License,
  4. // Version 1.0. (See http://www.boost.org/LICENSE_1_0.txt).
  5. //------------------------------------------------------------------------------
  6. // See macro_type_arugment.hpp in this directory for details.
  7. #include <string>
  8. #include <typeinfo>
  9. #include <iostream>
  10. #include <boost/mpl/begin_end.hpp>
  11. #include <boost/mpl/deref.hpp>
  12. #include "macro_type_args.hpp"
  13. #define TYPE_NAME(parenthesized_type) \
  14. typeid(BOOST_EXAMPLE_MACRO_TYPE_ARGUMENT(parenthesized_type)).name()
  15. namespace example
  16. {
  17. namespace mpl = boost::mpl;
  18. template<class Curr, class End>
  19. struct mpl_seq_to_string_impl
  20. {
  21. static std::string get(std::string const & prev)
  22. {
  23. typedef typename mpl::next<Curr>::type next_pos;
  24. typedef typename mpl::deref<Curr>::type type;
  25. return mpl_seq_to_string_impl<next_pos,End>::get(
  26. prev + (prev.empty()? '\0' : ',') + typeid(type).name() );
  27. }
  28. };
  29. template<class End>
  30. struct mpl_seq_to_string_impl<End, End>
  31. {
  32. static std::string get(std::string const & prev)
  33. {
  34. return prev;
  35. }
  36. };
  37. template<class Seq>
  38. std::string mpl_seq_to_string()
  39. {
  40. typedef typename mpl::begin<Seq>::type begin;
  41. typedef typename mpl::end<Seq>::type end;
  42. return mpl_seq_to_string_impl<begin, end>::get("");
  43. }
  44. }
  45. #define TYPE_NAMES(parenthesized_types) \
  46. ::example::mpl_seq_to_string< \
  47. BOOST_EXAMPLE_MACRO_TYPE_LIST_ARGUMENT(parenthesized_types) >()
  48. int main()
  49. {
  50. std::cout << TYPE_NAME((int)) << std::endl;
  51. std::cout << TYPE_NAMES((int,char)) << std::endl;
  52. std::cout << TYPE_NAMES((int,char,long)) << std::endl;
  53. }