implementation.qbk 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. [/
  2. / Copyright (c) 2008 Eric Niebler
  3. /
  4. / Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. / file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. /]
  7. [section:implementation Appendix D: Implementation Notes]
  8. [section:sfinae Quick-n-Dirty Type Categorization]
  9. Much has already been written about dispatching on type traits using
  10. SFINAE (Substitution Failure Is Not An Error) techniques in C++. There
  11. is a Boost library, Boost.Enable_if, to make the technique idiomatic.
  12. Proto dispatches on type traits extensively, but it doesn't use
  13. `enable_if<>` very often. Rather, it dispatches based on the presence
  14. or absence of nested types, often typedefs for void.
  15. Consider the implementation of `is_expr<>`. It could have been written
  16. as something like this:
  17. template<typename T>
  18. struct is_expr
  19. : is_base_and_derived<proto::some_expr_base, T>
  20. {};
  21. Rather, it is implemented as this:
  22. template<typename T, typename Void = void>
  23. struct is_expr
  24. : mpl::false_
  25. {};
  26. template<typename T>
  27. struct is_expr<T, typename T::proto_is_expr_>
  28. : mpl::true_
  29. {};
  30. This relies on the fact that the specialization will be preferred
  31. if `T` has a nested `proto_is_expr_` that is a typedef for `void`.
  32. All Proto expression types have such a nested typedef.
  33. Why does Proto do it this way? The reason is because, after running
  34. extensive benchmarks while trying to improve compile times, I have
  35. found that this approach compiles faster. It requires exactly one
  36. template instantiation. The other approach requires at least 2:
  37. `is_expr<>` and `is_base_and_derived<>`, plus whatever templates
  38. `is_base_and_derived<>` may instantiate.
  39. [endsect]
  40. [section:function_arity Detecting the Arity of Function Objects]
  41. In several places, Proto needs to know whether or not a function
  42. object `Fun` can be called with certain parameters and take a
  43. fallback action if not. This happens in _callable_context_ and
  44. in the _call_ transform. How does Proto know? It involves some
  45. tricky metaprogramming. Here's how.
  46. Another way of framing the question is by trying to implement
  47. the following `can_be_called<>` Boolean metafunction, which
  48. checks to see if a function object `Fun` can be called with
  49. parameters of type `A` and `B`:
  50. template<typename Fun, typename A, typename B>
  51. struct can_be_called;
  52. First, we define the following `dont_care` struct, which has an
  53. implicit conversion from anything. And not just any implicit
  54. conversion; it has a ellipsis conversion, which is the worst possible
  55. conversion for the purposes of overload resolution:
  56. struct dont_care
  57. {
  58. dont_care(...);
  59. };
  60. We also need some private type known only to us with an overloaded
  61. comma operator (!), and some functions that detect the presence of
  62. this type and return types with different sizes, as follows:
  63. struct private_type
  64. {
  65. private_type const &operator,(int) const;
  66. };
  67. typedef char yes_type; // sizeof(yes_type) == 1
  68. typedef char (&no_type)[2]; // sizeof(no_type) == 2
  69. template<typename T>
  70. no_type is_private_type(T const &);
  71. yes_type is_private_type(private_type const &);
  72. Next, we implement a binary function object wrapper with a very
  73. strange conversion operator, whose meaning will become clear later.
  74. template<typename Fun>
  75. struct funwrap2 : Fun
  76. {
  77. funwrap2();
  78. typedef private_type const &(*pointer_to_function)(dont_care, dont_care);
  79. operator pointer_to_function() const;
  80. };
  81. With all of these bits and pieces, we can implement `can_be_called<>` as
  82. follows:
  83. template<typename Fun, typename A, typename B>
  84. struct can_be_called
  85. {
  86. static funwrap2<Fun> &fun;
  87. static A &a;
  88. static B &b;
  89. static bool const value = (
  90. sizeof(no_type) == sizeof(is_private_type( (fun(a,b), 0) ))
  91. );
  92. typedef mpl::bool_<value> type;
  93. };
  94. The idea is to make it so that `fun(a,b)` will always compile by adding
  95. our own binary function overload, but doing it in such a way that we can
  96. detect whether our overload was selected or not. And we rig it so that
  97. our overload is selected if there is really no better option. What follows
  98. is a description of how `can_be_called<>` works.
  99. We wrap `Fun` in a type that has an implicit conversion to a pointer to
  100. a binary function. An object `fun` of class type can be invoked as
  101. `fun(a, b)` if it has such a conversion operator, but since it involves
  102. a user-defined conversion operator, it is less preferred than an
  103. overloaded `operator()`, which requires no such conversion.
  104. The function pointer can accept any two arguments by virtue
  105. of the `dont_care` type. The conversion sequence for each argument is
  106. guaranteed to be the worst possible conversion sequence: an implicit
  107. conversion through an ellipsis, and a user-defined conversion to
  108. `dont_care`. In total, it means that `funwrap2<Fun>()(a, b)` will
  109. always compile, but it will select our overload only if there really is
  110. no better option.
  111. If there is a better option --- for example if `Fun` has an overloaded
  112. function call operator such as `void operator()(A a, B b)` --- then
  113. `fun(a, b)` will resolve to that one instead. The question now is how
  114. to detect which function got picked by overload resolution.
  115. Notice how `fun(a, b)` appears in `can_be_called<>`: `(fun(a, b), 0)`.
  116. Why do we use the comma operator there? The reason is because we are
  117. using this expression as the argument to a function. If the return type
  118. of `fun(a, b)` is `void`, it cannot legally be used as an argument to
  119. a function. The comma operator sidesteps the issue.
  120. This should also make plain the purpose of the overloaded comma operator
  121. in `private_type`. The return type of the pointer to function is
  122. `private_type`. If overload resolution selects our overload, then the
  123. type of `(fun(a, b), 0)` is `private_type`. Otherwise, it is `int`.
  124. That fact is used to dispatch to either overload of `is_private_type()`,
  125. which encodes its answer in the size of its return type.
  126. That's how it works with binary functions. Now repeat the above process
  127. for functions up to some predefined function arity, and you're done.
  128. [endsect]
  129. [/
  130. [section:ppmp_vs_tmp Avoiding Template Instiations With The Preprocessor]
  131. TODO
  132. [endsect]
  133. ]
  134. [endsect]