inherit_linearly.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright Aleksey Gurtovoy 2002-2004
  2. //
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/mpl for documentation.
  8. // $Id$
  9. // $Date$
  10. // $Revision$
  11. #include <boost/mpl/inherit_linearly.hpp>
  12. #include <boost/mpl/int.hpp>
  13. #include <boost/mpl/list.hpp>
  14. #include <iostream>
  15. namespace mpl = boost::mpl;
  16. using namespace mpl::placeholders;
  17. template< typename Base, typename T >
  18. struct tuple_part
  19. : Base
  20. {
  21. typedef tuple_part type; // note the typedef
  22. typedef typename Base::index::next index;
  23. friend T& field(tuple_part& t, index) { return t.field_; }
  24. T field_;
  25. };
  26. struct empty_tuple
  27. {
  28. typedef mpl::int_<-1> index;
  29. };
  30. typedef mpl::inherit_linearly<
  31. mpl::list<int,char const*,bool>
  32. , tuple_part<_,_>
  33. , empty_tuple
  34. >::type my_tuple;
  35. int main()
  36. {
  37. my_tuple t;
  38. field(t, mpl::int_<0>()) = -1;
  39. field(t, mpl::int_<1>()) = "text";
  40. field(t, mpl::int_<2>()) = false;
  41. std::cout
  42. << field(t, mpl::int_<0>()) << '\n'
  43. << field(t, mpl::int_<1>()) << '\n'
  44. << field(t, mpl::int_<2>()) << '\n'
  45. ;
  46. return 0;
  47. }