Skip to main content

Functional Programming with C++ Template Metaprograms

  • Chapter
Book cover Central European Functional Programming School (CEFP 2009)

Part of the book series: Lecture Notes in Computer Science ((LNTCS,volume 6299))

Included in the following conference series:

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.

This is a preview of subscription content, log in via an institution to check access.

Access this chapter

Chapter
USD 29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD 39.99
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
Softcover Book
USD 54.99
Price excludes VAT (USA)
  • Compact, lightweight edition
  • Dispatched in 3 to 5 business days
  • Free shipping worldwide - see info

Tax calculation will be finalised at checkout

Purchases are for personal use only

Institutional subscriptions

Preview

Unable to display preview. Download preview PDF.

Unable to display preview. Download preview PDF.

References

  1. Abrahams, D., Gurtovoy, A.: C++ template metaprogramming, Concepts, Tools, and Techniques from Boost and Beyond. Addison-Wesley, Boston (2004)

    Google Scholar 

  2. Alexandrescu, A.: Modern C++ Design: Generic Programming and Design Patterns Applied. Addison-Wesley, Reading (2001)

    Google Scholar 

  3. ANSI/ISO C++ Committee. Programming Languages – C++. ISO/IEC 14882:1998(E). American National Standards Institute (1998)

    Google Scholar 

  4. Bohm, C., Jacopini, G.: Flow Diagrams, Turing Machines and Languages with Only Two Formation Rules. Communications of the ACM 9(5), 366–371 (1966)

    Article  MATH  Google Scholar 

  5. 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)

    Chapter  Google Scholar 

  6. 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)

    Chapter  Google Scholar 

  7. 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)

    Chapter  Google Scholar 

  8. Czarnecki, K., Eisenecker, U.W.: Generative Programming: Methods, Tools and Applications. Addison-Wesley, Reading (2000)

    Google Scholar 

  9. 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)

    Google Scholar 

  10. 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)

    Chapter  Google Scholar 

  11. Karlsson, B.: Beyond the C++ Standard Library, A Introduction to Boost. Addison-Wesley, Reading (2005)

    Google Scholar 

  12. Koopman, P., Plasmeijer, R., van Eeekelen, M., Smetsers, S.: Functional programming in Clean (2002)

    Google Scholar 

  13. 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)

    Chapter  Google Scholar 

  14. Musser, D.R., Stepanov, A.A.: Algorithm-oriented Generic Libraries. Software-practice and experience 27(7), 623–642 (1994)

    Article  Google Scholar 

  15. McNamara, B., Smaragdakis, Y.: Functional programming in C++. In: Proceedings of the Fifth ACM SIGPLAN International Conference on Functional Programming, pp. 118–129 (2000)

    Google Scholar 

  16. McNamara, B., Smaragdakis, Y.: Static interfaces in C++. In: First C++ Template Programming Workshop, Erfurt (October 2000)

    Google Scholar 

  17. Jones, S.L.P.: The Implementation of Functional Languages, pp. 4–45. Prentice-Hall, Englewood Cliffs (1987)

    Google Scholar 

  18. 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)

    Google Scholar 

  19. 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)

    Google Scholar 

  20. Siek, J., Lumsdaine, A.: Concept checking: Binding parametric polymorphism in C++. In: First C++ Template Programming Workshop, Erfurt (October 2000)

    Google Scholar 

  21. 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)

    Google Scholar 

  22. Siek, J.: A Language for Generic Programming. PhD thesis, Indiana University (August 2005)

    Google Scholar 

  23. 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)

    Google Scholar 

  24. 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)

    Google Scholar 

  25. Sipos, Á.: Effective development of C++ Template Metaprograms. PhD thesis. Eötvös Loránd University, Budapest, Hungary (2009)

    Google Scholar 

  26. Stroustrup, B.: The C++ Programming Language Special Edition. Addison-Wesley, Reading (2000)

    MATH  Google Scholar 

  27. 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)

    Google Scholar 

  28. Unruh, E.: Prime number computation. ANSI X3J16-94-0075/ISO WG21-462

    Google Scholar 

  29. Vandevoorde, D., Josuttis, N.M.: C++ Templates: The Complete Guide. Addison-Wesley, Reading (2003)

    Google Scholar 

  30. 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)

    Google Scholar 

  31. Veldhuizen, T.: Using C++ Template Metaprograms. C++ Report 7(4), 36–43 (1995)

    Google Scholar 

  32. Veldhuizen, T.: Expression Templates. C++ Report 7(5), 26–31 (1995)

    Google Scholar 

  33. 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)

    Google Scholar 

  34. 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)

    Chapter  Google Scholar 

  35. 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)

    Chapter  Google Scholar 

  36. Boost Concept checking, http://www.boost.org/libs/concept_check/concept_check.htm

  37. Boost Metaprogramming library, http://www.boost.org/libs/mpl/doc/index.html

  38. The boost lambda library, http://www.boost.org/doc/libs/1_40_0/doc/html/lambda.html

  39. Glasgow Haskell Compiler, http://www.haskell.org/ghc/

  40. York Haskell Compiler, http://community.haskell.org/~ndm/yhc/

Download references

Author information

Authors and Affiliations

Authors

Editor information

Editors and Affiliations

Rights and permissions

Reprints 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)

Publish with us

Policies and ethics