Abstract
Template metaprogramming is an emerging new direction of generative programming. With the clever definitions of templates we can force the C++ compiler to execute algorithms at compilation time. Among the application areas of template metaprograms are the expression templates, static interface checking, code optimization with adaption, language embedding and active libraries. However, as template metaprogramming was not an original design goal, the C++ language is not capable of elegant expression of metaprograms. The complicated syntax leads to the creation of code that is hard to write, understand and maintain. Although template metaprogramming has a strong relationship with functional programming, this is not reflected in the language syntax and existing libraries. In this paper we give a short and incomplete introduction to C++ templates and the basics of template metaprogramming. We will enlight the role of template metaprograms, and some important and widely used idioms. We give an overview of the possible application areas as well as debugging and profiling techniques. We suggest a pure functional style programming interface for C++ template metaprograms in the form of embedded Haskell code which is transformed to standard compliant C++ source.
Access this chapter
Tax calculation will be finalised at checkout
Purchases are for personal use only
Preview
Unable to display preview. Download preview PDF.
References
Abrahams, D., Gurtovoy, A.: C++ template metaprogramming, Concepts, Tools, and Techniques from Boost and Beyond. Addison-Wesley, Boston (2004)
Alexandrescu, A.: Modern C++ Design: Generic Programming and Design Patterns Applied. Addison-Wesley, Reading (2001)
ANSI/ISO C++ Committee. Programming Languages – C++. ISO/IEC 14882:1998(E). American National Standards Institute (1998)
Bohm, C., Jacopini, G.: Flow Diagrams, Turing Machines and Languages with Only Two Formation Rules. Communications of the ACM 9(5), 366–371 (1966)
Bravenboer, M., Vermaas, R., Vinju, J., Visser, E.: Generalized Type-Based Disambiguation of Meta Programs with Concrete Object Syntax. In: Glück, R., Lowry, M. (eds.) GPCE 2005. LNCS, vol. 3676, pp. 157–172. Springer, Heidelberg (2005)
Brus, T.H., van Eekelen, C.J.D., van Leer, M.O., Plasmeijer, M.J.: CLEAN: A language for functional graph rewriting. In: Kahn, G. (ed.) FPCA 1987. LNCS, vol. 274, pp. 364–384. Springer, Heidelberg (1987)
Csörnyei, Z., Dévai, G.: An introduction to the lambda-calculus. In: Horváth, Z., Plasmeijer, R., Soós, A., Zsók, V. (eds.) Central European Functional Programming School. LNCS, vol. 5161, pp. 87–111. Springer, Heidelberg (2008)
Czarnecki, K., Eisenecker, U.W.: Generative Programming: Methods, Tools and Applications. Addison-Wesley, Reading (2000)
Garcia, R., Järvi, J., Lumsdaine, A., Siek, J., Willcock, J.: A Comparative Study of Language Support for Generic Programming. In: Proceedings of the 18th ACM SIGPLAN OOPSLA, pp. 115–134 (2003)
Juhász, Z., Sipos, Á., Porkoláb, Z.: Implementation of a Finite State Machine with Active Libraries in C++. In: Lämmel, R., Visser, J., Saraiva, J. (eds.) Generative and Transformational Techniques in Software Engineering II. LNCS, vol. 5235, pp. 474–488. Springer, Heidelberg (2008)
Karlsson, B.: Beyond the C++ Standard Library, A Introduction to Boost. Addison-Wesley, Reading (2005)
Koopman, P., Plasmeijer, R., van Eeekelen, M., Smetsers, S.: Functional programming in Clean (2002)
Mitchell, N., Runciman, C.: A Supercompiler for Core Haskell. In: Chitil, O., Horváth, Z., Zsók, V. (eds.) IFL 2007. LNCS, vol. 5083, pp. 147–164. Springer, Heidelberg (2008)
Musser, D.R., Stepanov, A.A.: Algorithm-oriented Generic Libraries. Software-practice and experience 27(7), 623–642 (1994)
McNamara, B., Smaragdakis, Y.: Functional programming in C++. In: Proceedings of the Fifth ACM SIGPLAN International Conference on Functional Programming, pp. 118–129 (2000)
McNamara, B., Smaragdakis, Y.: Static interfaces in C++. In: First C++ Template Programming Workshop, Erfurt (October 2000)
Jones, S.L.P.: The Implementation of Functional Languages, pp. 4–45. Prentice-Hall, Englewood Cliffs (1987)
Porkoláb, Z., Mihalicza, J., Sipos, Á.: Debugging C++ template metaprograms. In: Jarzabek, S., Schmidt, D.C., Veldhuizen, T.L. (eds.) Proceedings Generative Programming and Component Engineering, 5th International Conference, GPCE 2006, Portland, Oregon, USA, October 22-26, pp. 255–264. ACM, New York (2006)
Gregor, D., Järvi, J., Siek, J.G., Reis, G.D., Stroustrup, B., Lumsdaine, A.: Concepts: Linguistic Support for Generic Programming in C++. In: Proceedings of the 2006 ACM SIGPLAN Conference on Object-Oriented Programming, Systems, Languages, and Applications, OOPSLA 2006 (October 2006)
Siek, J., Lumsdaine, A.: Concept checking: Binding parametric polymorphism in C++. In: First C++ Template Programming Workshop, Erfurt (October 2000)
Siek, J., Lumsdaine, A.: Essential Language Support for Generic Programming. In: Proceedings of the ACM SIGPLAN 2005 Conference on Programming Language Design and Implementation, New York, NY, USA, pp. 73–84 (2005)
Siek, J.: A Language for Generic Programming. PhD thesis, Indiana University (August 2005)
Sinkovics, Á., Porkoláb, Z.: Expressing C++ Template Metaprograms as Lambda expressions. In: Horváth, Z., Zsók, V., Achten, P., Koopman, P. (eds.) Tenth symposium on Trends in Functional Programming (TFP 2009), Komarno, Slovakia, June 2 - 4, pp. 97–111 (2009)
Sipos, Á., Porkoláb, Z., Zsók, V.: Meta<fun> – Towards a functional-style interface for C++ template metaprograms. In: Frentiu, et al. (eds.) Studia Universitatis Babes-Bolyai Informatica LIII,Cluj-Napoca, pp. 55–66 (Febraury 2008)
Sipos, Á.: Effective development of C++ Template Metaprograms. PhD thesis. Eötvös Loránd University, Budapest, Hungary (2009)
Stroustrup, B.: The C++ Programming Language Special Edition. Addison-Wesley, Reading (2000)
Torgersen, M., Hansen, C.P., Ernst, E., Ahe, P., Bracha, G., Gafter, N.: Adding Wildcards to the Java Programming Language. In: Proceedings of the 2004 ACM Symposium on Applied Computing (SAC) 2004, pp. 1289–1296 (2004)
Unruh, E.: Prime number computation. ANSI X3J16-94-0075/ISO WG21-462
Vandevoorde, D., Josuttis, N.M.: C++ Templates: The Complete Guide. Addison-Wesley, Reading (2003)
Veldhuizen, T.L., Gannon, D.: Active libraries: Rethinking the roles of compilers and libraries. In: Proceedings of the SIAM Workshop on Object Oriented Methods for Inter-operable Scientic and Engineering Computing (OO 1998), pp. 21–23. SIAM Press, Philadelphia (1998)
Veldhuizen, T.: Using C++ Template Metaprograms. C++ Report 7(4), 36–43 (1995)
Veldhuizen, T.: Expression Templates. C++ Report 7(5), 26–31 (1995)
Zalewski, M., Priesnitz, A.P., Ionescu, C., Botta, N., Schupp, S.: Multi-language library development: From Haskell type classes to C++ concepts. In: MPOOL 2007 Ecoop workshp (2007)
Zólyomi, I., Porkoláb, Z., Kozsik, T.: An extension to the subtype relationship in C++. In: Pfenning, F., Smaragdakis, Y. (eds.) GPCE 2003. LNCS, vol. 2830, pp. 209–227. Springer, Heidelberg (2003)
Zólyomi, I., Porkoláb, Z.: Towards a template introspection library. In: Karsai, G., Visser, E. (eds.) GPCE 2004. LNCS, vol. 3286, pp. 266–282. Springer, Heidelberg (2004)
Boost Concept checking, http://www.boost.org/libs/concept_check/concept_check.htm
Boost Metaprogramming library, http://www.boost.org/libs/mpl/doc/index.html
The boost lambda library, http://www.boost.org/doc/libs/1_40_0/doc/html/lambda.html
Glasgow Haskell Compiler, http://www.haskell.org/ghc/
York Haskell Compiler, http://community.haskell.org/~ndm/yhc/
Author information
Authors and Affiliations
Editor information
Editors and Affiliations
Rights and permissions
Copyright information
© 2010 Springer-Verlag Berlin Heidelberg
About this chapter
Cite this chapter
Porkoláb, Z. (2010). Functional Programming with C++ Template Metaprograms. In: Horváth, Z., Plasmeijer, R., Zsók, V. (eds) Central European Functional Programming School. CEFP 2009. Lecture Notes in Computer Science, vol 6299. Springer, Berlin, Heidelberg. https://doi.org/10.1007/978-3-642-17685-2_9
Download citation
DOI: https://doi.org/10.1007/978-3-642-17685-2_9
Publisher Name: Springer, Berlin, Heidelberg
Print ISBN: 978-3-642-17684-5
Online ISBN: 978-3-642-17685-2
eBook Packages: Computer ScienceComputer Science (R0)