construction.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Boost.TypeErasure library
  2. //
  3. // Copyright 2011 Steven Watanabe
  4. //
  5. // Distributed under the Boost Software License Version 1.0. (See
  6. // accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // $Id$
  10. #include <boost/type_erasure/any.hpp>
  11. #include <boost/type_erasure/tuple.hpp>
  12. #include <boost/type_erasure/binding_of.hpp>
  13. #include <string>
  14. #include <vector>
  15. namespace mpl = boost::mpl;
  16. using namespace boost::type_erasure;
  17. void construction1() {
  18. //[construction1
  19. /*`
  20. The library provides the __constructible concept to
  21. allow an __any to capture constructors. The single
  22. template argument should be a function signature.
  23. The return type must be a placeholder specifying
  24. the type to be constructed. The arguments are
  25. the arguments of the constructor.
  26. */
  27. typedef mpl::vector<
  28. copy_constructible<_a>,
  29. copy_constructible<_b>,
  30. copy_constructible<_c>,
  31. constructible<_a(const _b&, const _c&)>
  32. > construct;
  33. typedef mpl::map<
  34. mpl::pair<_a, std::vector<double> >,
  35. mpl::pair<_b, std::size_t>,
  36. mpl::pair<_c, double>
  37. > types;
  38. any<construct, _b> size(std::size_t(10), make_binding<types>());
  39. any<construct, _c> val(2.5, make_binding<types>());
  40. any<construct, _a> v(size, val);
  41. // v holds std::vector<double>(10, 2.5);
  42. //]
  43. }
  44. void construction3() {
  45. //[construction3
  46. /*`
  47. Now, suppose that we want a default constructor?
  48. We can't have the default constructor of __any
  49. call the default constructor of the contained type,
  50. because it would have no way of knowing what the
  51. contained type is. So, we'll need to pass
  52. the placeholder binding information explicitly.
  53. */
  54. typedef mpl::vector<
  55. copy_constructible<>,
  56. constructible<_self()>
  57. > construct;
  58. any<construct> x(std::string("Test"));
  59. any<construct> y(binding_of(x)); // y == ""
  60. //]
  61. }
  62. void construction4() {
  63. //[construction4
  64. /*`
  65. This method is not restricted to the default constructor. If
  66. the constructor takes arguments, they can be passed after the
  67. bindings.
  68. */
  69. typedef mpl::vector<
  70. copy_constructible<>,
  71. constructible<_self(std::size_t, char)>
  72. > construct;
  73. any<construct> x(std::string("Test"));
  74. any<construct> y(binding_of(x), 5, 'A');
  75. //]
  76. }
  77. //[construction
  78. //` (For the source of the examples in this section see
  79. //` [@boost:/libs/type_erasure/example/construction.cpp construction.cpp])
  80. //` [construction1]
  81. //` [construction3]
  82. //` [construction4]
  83. //]