inherit_multiply.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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.hpp>
  12. #include <boost/mpl/inherit_linearly.hpp>
  13. #include <boost/mpl/list.hpp>
  14. #include <iostream>
  15. namespace mpl = boost::mpl;
  16. using namespace mpl::placeholders;
  17. template< typename T >
  18. struct tuple_field
  19. {
  20. typedef tuple_field type; // note the typedef
  21. T field_;
  22. };
  23. template< typename T >
  24. inline
  25. T& field(tuple_field<T>& t)
  26. {
  27. return t.field_;
  28. }
  29. typedef mpl::inherit_linearly<
  30. mpl::list<int,char const*,bool>
  31. , mpl::inherit< _1, tuple_field<_2> >
  32. >::type my_tuple;
  33. int main()
  34. {
  35. my_tuple t;
  36. field<int>(t) = -1;
  37. field<char const*>(t) = "text";
  38. field<bool>(t) = false;
  39. std::cout
  40. << field<int>(t) << '\n'
  41. << field<char const*>(t) << '\n'
  42. << field<bool>(t) << '\n'
  43. ;
  44. return 0;
  45. }