Abstract
C++ template metaprogramming is a form of strict functional programming, with a notable absence of intrinsic support for elementary higher-order operations. We describe a variadic template metaprogramming library which offers a model of implicitly curried, left-associative metafunction application through juxtaposition; inspired by languages such as Haskell, OCaml and F\(^\sharp \). New and existing traits and metafunctions, constructed according to conventional idioms, seemlessly take advantage of the framework’s features. Furthermore, a distinctive versatility is exposed, allowing a user to define higher-order metafunction classes using an equational definition syntax; without recourse to elaborate nested metafunctions. The primary type expression evaluator of the library is derived from a single application of an elementary folding combinator for type lists. The definition of the fold’s binary operator argument is therefore a focal point; and constructed mindful that substitution failure of a template parameter’s deduced type produces no compilation error. Two distinctive features of C++ metafunctions require particular consideration: zero argument metafunctions; and variadic metafunctions. We conclude by demonstrating characteristics of the library’s main evaluation metafunction in conjunction with the universal property of an updated right-fold combinator, to compose a range of metafunctions including map, reverse, left-fold, and the Ackermann function.
Access this chapter
Tax calculation will be finalised at checkout
Purchases are for personal use only
Notes
- 1.
Often map is named after the standard C++ runtime function. A fold is more often named ; with provided as an alias.
- 2.
This is a second, distinct usage of the keyword. The keyword is not permitted in this context; though is.
- 3.
The disambiguator informs the compiler that a dependent name following the operator, refers to a type [37, p. 228].
- 4.
is a keyword used to query the type of an expression.
- 5.
The type expression will evaluate to a type; an instantiation of , and hence also of . The braces which follow this expression will aggregate-initialise a object before using its conversion operator member to provide an value as the multiplier, with the multiplicand.
- 6.
Regarding a template parameter pack: note that its declaration is preceded by an ellipsis; while its expansion is followed by one [37, p. 188].
- 7.
The disambiguator informs the compiler that a dependent name following a , , or operator, refers to a template [37, p. 230].
- 8.
Discussion regarding the virtue of this restriction remains an active topic within the ISO C++ Standards Committee; identified as core issue 1430 [25].
- 9.
The class template makes use of a powerful C++ feature wherein template substitution failure is not an error (SFINAE). Furthermore, is defined as “SFINAE-friendly”. For example, as and have no common type, neither , nor the equivalent , has a member. The failed instantiation of the specialisation with resolves to the primary template; leaving subsequent access to the absent member only a substitution error. The alternative is a hard error. C++17’s is an alias template helpful in such contexts; provided with zero or more valid type template arguments, the aggregate instantiates to [37, p. 420].
- 10.
A variant, , accepting class template arguments, is defined in Appendix A.1.
- 11.
A template parameter pack is used by rather than a type list; and hence it is not strictly isomorphic to the Haskell fold above; this is however only part of the implementation, not of the public API.
- 12.
A would do, though the minimal class template shown is sufficient for compile-time calculations.
- 13.
The Pointfree.io website is an excellent resource for producing such translations.
- 14.
A Curtains definition of the Haskell combinator is provided in Appendix A.4.
References
International Standard ISO/IEC 14882:1998(E): Programming Languages – C++ (1998)
International Standard ISO/IEC 14882:2011: Information Technology - Programming Languages - C++ (2011)
International Standard ISO/IEC 19217:2015: Information Technology - Programming Languages - C++ Extensions for Concepts (2015)
Abrahams, D., Gurtovoy, A.: C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond. AW Prof., Boston (2004)
Alexandrescu, A.: Modern C++ Design: Generic Programming and Design Patterns Applied. Addison-Wesley Longman Publishing, Boston (2001)
Austern, M.H.: Generic Programming and the STL: Using and Extending the C++ Standard Template Library. AW Prof. Comp. Series. AW Longman Publ. Co., Boston (1998)
Brown, W.E.: Proposing standard library support for the C++ detection idiom. Technical report, ISO WG21 C++ Working Group, April 2015. http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4436.pdf
Deppe, N., Douwes, C., Fresk, E., Holmes, O., Poelen, J.: Kvasir::mpl (2017). https://github.com/kvasir-io/mpl
Dionne, L.: Hana (2013). https://github.com/boostorg/hana
Dutra, B.: Metal (2018). https://github.com/brunocodutra/metal
Escribá, V.J.B.: P0343R1: Meta-programming high-order functions. Technical report, ISO WG21 C++ Library Evolution Working Group (2016)
Gil, J., Gutterman, Z.: Compile time symbolic derivation with C++ templates. In: Proceedings of the 4th Conference on USENIX Conference on Object-Oriented Technologies and Systems - COOTS 1998, vol. 4, pp. 18–18. USENIX Association, Berkeley (1998). http://dl.acm.org/citation.cfm?id=1268009.1268027
Golodotz, S.: Functional Programming Using C++ Templates (Part 1), October 2007. http://accu.org/index.php/journals/1422
Guennebaud, G., Jacob, B., et al.: Eigen v3 (2010). http://eigen.tuxfamily.org
Haeri, S.H., Schupp, S.: Functional Metaprogramming in C++ and cross-lingual development with Haskell. Technical report, Uni. Kansas, October 2011. Draft Proceeding of 23rd Symposium on Implementation and Application of Functional Languages, ITTC-FY2012-TR-29952012-01
Haeri, S.H., Schupp, S., Hüser, J.: Using functional languages to facilitate C++ Metaprogramming. In: Proceedings of the 8th ACM SIGPLAN Workshop on Generic Programming, WGP 2012, pp. 33–44. ACM (2012)
Holmes, O., Kurdej, M., Poelen, J.: Brigand Meta-programming Library (2015). https://github.com/edouarda/brigand
Hutton, G.: A tutorial on the universality and expressiveness of fold. J. Funct. Program. 9(4), 355–372 (1999)
Lincke, D., Schupp, S., Ionescu, C.: Functional prototypes for generic C++ libraries: a transformational approach based on higher-order, typed signatures. Int. J. Soft. Tools Tech. Transf. 17(1), 91–105 (2015). https://doi.org/10.1007/s10009-014-0299-0
Lumsdaine, A., Siek, J., Lee, L.Q.: The Boost Graph Library: User Guide and Reference Manual. Addison-Wesley Longman Publishing Co., Inc, Boston (2002)
Mach, S.: Metatrace (2010). https://github.com/phresnel/metatrace
Marlow, S.: Haskell 2010 Language Report (2010)
McBride, C.: Faking it: simulating dependent types in Haskell. J. Funct. Program. 12(5), 375–392 (2002)
Meijer, E., Fokkinga, M., Paterson, R.: Functional programming with bananas, lenses, envelopes and barbed wire. In: Hughes, J. (ed.) FPCA 1991. LNCS, vol. 523, pp. 124–144. Springer, Heidelberg (1991). https://doi.org/10.1007/3540543961_7
Merrill, J.: C++ Core Issue 1430 (2011). http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1430
Milewski, B.: Bartosz Milewski’s Programming Cafe. http://bartoszmilewski.wordpress.com
Niebler, E.: Meta: A Tiny Metaprogramming Library (2014). https://ericniebler.github.io/meta/index.html
Porkoláb, Z., Sinkovics, Á.: C++ template metaprogramming with embedded Haskell. In: Proceedings of 8th International Conference on Generative Programming & Component Engineering (GPCE 2009), pp. 99–108. ACM, New York (2009)
Sankel, D.: Algebraic Data Types Series, C++ Next: The Next Generation of C++. http://cpp-next.com/archive/2010/07/algebraic-data-types/
Sinkovics, Á.: Functional extensions to the boost metaprogram library. Electron. Notes Theor. Comput. Sci. 264(5), 85–101 (2011). Proceedings of 2nd Workshop on Generative Technologies. https://doi.org/10.1016/j.entcs.2011.06.006
Sinkovics, Á.: Nested lamda expressions with let expressions in C++ template metaprograms, pp. 63–76 (2011)
Sinkovics, Á., Porkoláb, Z.: Expressing C++ template metaprograms as lambda expressions. In: TFP, pp. 1–15 (2009)
Sinkovics, Á., Porkoláb, Z.: Metaparse: Compile-Time Parsing with Template Metaprogramming. Aspen, USA (2012). https://github.com/boostcon/cppnow_presentations_2012/blob/master/papers/metaparse_paper.pdf
Sipos, Á., Porkoláb, Z., Pataki, N., Zsók, V.: Meta<Fun>: towards a functional-style interface for C++ template metaprograms. Technical report, Eötvös Loránd Uni, Fac. of Inf., Dept. Prog. Langs., Pázmány Péter sétány 1/C H-1117 Budapest, Hungary (2007)
Unruh, E.: Prime Number Computation (1994). ANSI X3J16-94-0075/ISO WG21-462
van Eekelen, M., de Mol, M.J.: Mixed lazy/strict graph semantics. In: Grelck, C., Huch, F. (eds.) 16th International Workshop on Implementation of Applied Functional Languages, IFL 2004, pp. 245–260. Technical report 0408, Christian-Albrechts-Universität zu Kiel, Lüebeck, Germany, September 2004
Vandevoorde, D., Josuttis, N.M., Gregor, D.: C++ Templates: The Complete Guide, 2nd edn. AW Prof., Boston (2018)
Veldhuizen, T.L.: Expression templates. C++ Rep. 7(5), 26–31 (1995)
Veldhuizen, T.L.: Scientific computing: C++ versus Fortran: C++ has more than caught up. Dr. Dobb’s J. Softw. Tools 22(11), 34, 36–38, 91 (1997)
Author information
Authors and Affiliations
Corresponding author
Editor information
Editors and Affiliations
A Appendix A
A Appendix A
1.1 A.1 Quotation for a Class Template Argument
1.2 A.2 Curry-Invoke with Arity
1.3 A.3 Conditional Invoke with Arity
1.4 A.4 Additional Utility Metafunctions
1.5 A.5 A Sample Variadic Metafunction:
1.6 A.6 Point-Free Reverse From a Right-Fold
1.7 A.7 Point-Free Left-Fold From a Right-Fold
Implementation derived from Hutton [18]; with assistance from the Pointfree.io website; which provides: .
1.8 A.8 The SKI Combinators
1.9 A.9 Point-Free Ackermann Function from a Right-Fold
Here and are used in lieu of and , for the Applicative instance of . The implementation is derived from Hutton [18]; with assistance from the Pointfree.io website.
1.10 A.10 Non-recursive Factorial for Use with the Fixpoint Combinator
1.11 A.11 Primitive Left-Fold from a C++17 Fold Expression
1.12 A.12 Reduction Steps of a Sample Curtains Expression
Rights and permissions
Copyright information
© 2019 Springer Nature Switzerland AG
About this paper
Cite this paper
Keir, P., Gozillon, A., Haeri, S.H. (2019). Intrinsic Currying for C++ Template Metaprograms. In: Pałka, M., Myreen, M. (eds) Trends in Functional Programming. TFP 2018. Lecture Notes in Computer Science(), vol 11457. Springer, Cham. https://doi.org/10.1007/978-3-030-18506-0_3
Download citation
DOI: https://doi.org/10.1007/978-3-030-18506-0_3
Published:
Publisher Name: Springer, Cham
Print ISBN: 978-3-030-18505-3
Online ISBN: 978-3-030-18506-0
eBook Packages: Computer ScienceComputer Science (R0)