From fdff99d48cddccedd4ec0fa3bdf8d6e1883c2a32 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Tue, 3 Mar 2015 20:12:32 +0100 Subject: Added bindings section to latest article draft --- ...e_as_a_metaphor_for_template_metaprogramming.md | 41 +++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) (limited to 'articles') diff --git a/articles/2015-02-27_using_scheme_as_a_metaphor_for_template_metaprogramming.md b/articles/2015-02-27_using_scheme_as_a_metaphor_for_template_metaprogramming.md index 4ab74ee..3781645 100644 --- a/articles/2015-02-27_using_scheme_as_a_metaphor_for_template_metaprogramming.md +++ b/articles/2015-02-27_using_scheme_as_a_metaphor_for_template_metaprogramming.md @@ -133,7 +133,46 @@ using Fold = Eval>; Hiding member type defintions behind template aliases enables most _higher_ functionality and applications built using it's functions to be written in a reasonably minimalistic - _Scheme_ like - fashion as we can see in e.g. the listing of `Every`. This also explains why `tav::Pair` recursively defines it's own type, as we would otherwise have to be quite careful where to resolve `tav::Eval`. -## Partial function application +### Bindings + +Not all programs are sensibly expressed as a nested chain of function calls if we want to reuse some values, separate functionality into appropriately named functions and hide complexity via private bindings. While in _Scheme_ one would use `let` and the like for this purpose, such functionality is not easily replicated in the context of template metaprogramming. This is why _TypeAsValue_ uses a alternate and more _C++ like_ solution to this problem by simply making use of the standard object oriented aspects of the language. + +~~~ +template < + template class Comparator, + typename Sequence +> +class quick_sort { + private: + using index = Divide, Size<2>>; + using pivot = Nth; + + using partitions = Partition< + Apply::template function, + DeleteNth + >; + + using lhs = Car; + using rhs = Cdr; + + public: + using type = Concatenate< + Eval>, + List, + Eval> + >; +}; + +template class Comparator> +struct quick_sort { + typedef void type; +}; +~~~ +{:.language-cpp} + +Above we can see the complete listing of the _Quick Sort_ implementation employed by the `tav::Sort` template alias. At first glance this looks rather different from a _Scheme_ program but if we look closer on could argue that it is a equivalent of a `let` binding where the _private_ section of `tav::detail::quick_sort` contains the bound constants and the _public_ section contains the body. + +### Partial function application ## Examples -- cgit v1.2.3