Guest User

Is it possible to iterate an mpl::vector at run time without instantiating the types in the vector

a guest
Feb 23rd, 2012
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. #include <boost/mpl/begin_end.hpp>
  2. #include <boost/mpl/next_prior.hpp>
  3. #include <boost/mpl/vector.hpp>
  4.  
  5. using namespace boost::mpl;
  6.  
  7.  
  8. namespace detail {
  9.  
  10. template < typename Begin, typename End, typename F >
  11. struct static_for_each
  12. {
  13. static void call( )
  14. {
  15. typedef typename Begin::type currentType;
  16.  
  17. F::template call< currentType >();
  18. static_for_each< typename next< Begin >::type, End, F >::call();
  19. }
  20. };
  21.  
  22.  
  23. template < typename End, typename F >
  24. struct static_for_each< End, End, F >
  25. {
  26. static void call( )
  27. {
  28. }
  29. };
  30.  
  31. } // namespace detail
  32.  
  33.  
  34. template < typename Sequence, typename F >
  35. void static_for_each( )
  36. {
  37. typedef typename begin< Sequence >::type begin;
  38. typedef typename end< Sequence >::type end;
  39.  
  40. detail::static_for_each< begin, end, F >::call();
  41. }
  42.  
  43. struct Foo
  44. {
  45. static void staticMemberFunction( )
  46. {
  47. std::cout << "Foo";
  48. }
  49. };
  50.  
  51.  
  52. struct Bar
  53. {
  54. static void staticMemberFunction( )
  55. {
  56. std::cout << "Bar";
  57. }
  58. };
  59.  
  60.  
  61. struct CallStaticMemberFunction
  62. {
  63. template < typename T >
  64. static void call()
  65. {
  66. T::staticMemberFunction();
  67. }
  68. };
  69.  
  70.  
  71. int main()
  72. {
  73. typedef vector< Foo, Bar > sequence;
  74.  
  75. static_for_each< sequence, CallStatic >(); // prints "FooBar"
  76. }
  77.  
  78. typedef boost::mpl::vector<type1*, type2*> container;
  79.  
  80. struct functor
  81. {
  82. template<typename T> void operator()(T*)
  83. {
  84. std::cout << "created " << typeid(T).name() << std::endl;
  85. }
  86. };
  87.  
  88. int main()
  89. {
  90. boost::mpl::for_each<container>(functor());
  91. }
  92.  
  93. #include <boost/mpl/for_each.hpp>
  94. #include <boost/mpl/identity.hpp>
  95. #include <boost/mpl/vector.hpp>
  96.  
  97. namespace mpl = boost::mpl;
  98.  
  99. struct DoStaticCall
  100. {
  101. template <class T>
  102. void operator() (mpl::identity<T>) const
  103. {
  104. T::StaticCall();
  105. }
  106. };
  107.  
  108. class A
  109. {
  110. A() {} // private constructor
  111. };
  112.  
  113. class B
  114. {
  115. B() {} // private constructor
  116. };
  117.  
  118. typedef boost::mpl::vector<A,B> AB;
  119.  
  120. int main()
  121. {
  122. boost::mpl::for_each<AB, mpl::make_identity<mpl::_> >(
  123. DoStaticCall()
  124. );
  125.  
  126. return 0;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment