Advertisement

Immutables in C++: Language Foundation for Functional Programming

  • Zoltán PorkolábEmail author
Chapter
Part of the Lecture Notes in Computer Science book series (LNCS, volume 10094)

Abstract

The C++ programming language is a multiparadigm language, with a rich set of procedural, object-oriented, generative and, since C++11, functional language elements. The language is also well-known for its capability to map certain semantic features into the language syntax; therefore, the compiler can reason about them at compile time. Supporting functional programming with immutables is one of such aspects: the programmer can mark immutable components and the compiler checks potential violation scenarios and also optimizes the code according to the constant expectations.

The paper targets the non-C++ programmer audience less familiar with the technical details of C++ immutables and functional elements, as well as those C++ programmers who are interested in the development of the newest standard. We will survey the functional programming features of modern C++. The various types of constants and immutable memory storage will be discussed as well as the rules of const correctness to enable the static type system to catch const violations. Const and static const members of classes represent support for immutables in object-oriented programming. Specific programming tools, like mutable and const_cast enable the programmer changing constness for exceptional cases. Constexpr and relaxed constexpr (since C++14) objects and functions as well as lambda expressions have recently been added to C++ to extend the language support for functional programming. We also discuss the fundamentals of C++ template metaprogramming, a pure functional paradigm operating at compile time working with immutable objects.

Understanding the immutable elements and the rich set of functional language features with their interactions can help programmers to implement safe, efficient and expressive C++ programs in functional style.

Keywords

C++ Functional programming Immutable Const Constexpr Template metaprogramming 

References

  1. 1.
    Abrahams, D., Gurtovoy, A.: C++ Template Metaprogramming, Concepts, Tools, and Techniques from Boost and Beyond, p. 400. Addison-Wesley, Boston (2004). ISBN 0321-22725-6Google Scholar
  2. 2.
    Alexandrescu, A.: Modern C++ Design: Generic Programming and Design Patterns Applied. Addison-Wesley, Boston (2001)Google Scholar
  3. 3.
    Allain, A.: Constexpr - Generalized Constant Expressions in C++11. http://www.cprogramming.com/c++11/c++11-compile-time-processing-with-constexpr.html
  4. 4.
    Austern, M.H.: Generic Programming and the STL: Using and Extending the C++ Standard Template Library. Addison-Wesley, Boston (1998)Google Scholar
  5. 5.
    Coplien, J.O.: Multi-Paradigm Design for C++. Addison-Wesley Longman Publishing Co., Inc., Boston (1998)Google Scholar
  6. 6.
    Czarnecki, K., Eisenecker, U.W.: Generative Programming: Methods, Tools and Applications. Addison-Wesley, Boston (2000)Google Scholar
  7. 7.
    Dami, L.: More functional reusability in C/C++/ Objective-C with curried functions. Object Composition, pp. 85–98. Centre Universitaire d’Informatique, University of Geneva, June 1991Google Scholar
  8. 8.
    Ellis, M., Stroustrup, B.: The Annotated C++ Reference Manual. Addison-Wesley, Boston (1990)Google Scholar
  9. 9.
    Fejerčák, V., Szabó, Cs., Bollin, A.: A software reverse engineering experience with the AMEISE legacy system. In: Electrical Engineering and Informatics 6: Proceedings of the Faculty of Electrical Engineering and Informatics of the Technical University of Košice, pp. 357–362. FEI TU, Košice (2015). ISBN 978-80-553-2178-3Google Scholar
  10. 10.
    Gil, Y., Lenz, K.: Simple and safe SQL queries with C++ templates. In: Consela, C., Lawall, J.L. (eds.) 6th International Conference on Generative Programming and Component Engineering, GPCE 2007, Salzburg, Austria, 1–3 October, pp. 13–24 (2007)Google Scholar
  11. 11.
    Hudak, P.: Conception, evolution, and application of functional programming languages. ACM Comput. Surv. 21(3), 359–411 (1989).  https://doi.org/10.1145/72551.72554CrossRefGoogle Scholar
  12. 12.
    The C++11 Standard: ISO International Standard, ISO/IEC 14882:2011(E) - Information technology - Programming languages - C++ (2011)Google Scholar
  13. 13.
    The C++14 Standard: ISO International Standard, ISO/IEC 14882:2014(E) - Programming Language C++ (2014)Google Scholar
  14. 14.
    Järvi, J., Powell, G., Lumsdaine, A.: The Lambda library: unnamed functions in C++. Softw. Pract. Exper. 33(3), 259–291 (2003).  https://doi.org/10.1002/spe.504CrossRefzbMATHGoogle Scholar
  15. 15.
    Järvi, J., Freeman, J.: C++ lambda expressions and closures. Sci. Comput. Program. 75(9), 762–772 (2010)MathSciNetCrossRefGoogle Scholar
  16. 16.
    Karlsson, B.: Beyond the C++ Standard Library, An Introduction to Boost. Addison-Wesley, Boston (2005)Google Scholar
  17. 17.
    Kernighan, B.W., Ritche, D.M.: The C Programming Language, vol. 2. Prentice-Hall, Englewood Cliffs (1988)Google Scholar
  18. 18.
    Kiselyov, O.: Functional style in C++: closures, late binding, and Lambda abstractions. In: Proceedings of the Third ACM SIGPLAN International Conference on Functional Programming, (ICFP 1998), p. 337. ACM, New York (1998).  https://doi.org/10.1145/289423.289464
  19. 19.
    Koopman, P., Plasmeijer, R., Achten, P.: An executable and testable semantics for iTasks. In: Scholz, S.-B., Chitil, O. (eds.) IFL 2008. LNCS, vol. 5836, pp. 212–232. Springer, Heidelberg (2011).  https://doi.org/10.1007/978-3-642-24452-0_12CrossRefGoogle Scholar
  20. 20.
    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
  21. 21.
    McNamara, B., Smaragdakis, Y.: Functional programming in C++ using the FC++ library. SIGPLAN Not. 36(4), 25–30 (2001)CrossRefGoogle Scholar
  22. 22.
    Meyers, S.: Effective STL – 50 Specific Ways to Improve Your Use of the Standard Template Library. Addison-Wesley, Boston (2001)Google Scholar
  23. 23.
    Meyers, S.: Effective Modern C++. O’Reilly Media, Sebastopol (2014). ISBN 978-1-4919-0399-5, ISBN 10 1-4919-0399-6Google Scholar
  24. 24.
    Mihalicza, J., Pataki, N., Porkoláb, Z.: Compiler support for profiling C++ template metaprograms. In: Proceedings of the 12th Symposium on Programming Languages and Software Tools (SPLST 2011), pp. 32–43, October 2011Google Scholar
  25. 25.
    Milewski, B.: Functional Data Structures in C++. C++Now, Aspen (2015). https://www.youtube.com/watch?v=OsB09djvfl4
  26. 26.
    Milewski, B.: C++ Ranges are Pure Monadic Goodness. B. Milewski’s blog. https://bartoszmilewski.com/2014/10/17/c-ranges-are-pure-monadic-goodness/
  27. 27.
    Musser, D.R., Stepanov, A.A.: Algorithm-oriented generic libraries. Softw.-Pract. Exper. 27(7), 623–642 (1994)CrossRefGoogle Scholar
  28. 28.
    Niebler, E.: Ranges for the Standard Library proposal, Rev. 1, N4128, 10 October 2014. http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4128.html
  29. 29.
    Pataki, N., Szűgyi, Z., Dévai, G.: C++ standard template library in a safer way. In: Proceedings of Workshop on Generative Technologies (WGT 2010), pp. 46–55 (2010)Google Scholar
  30. 30.
    Porkoláb, Z.: Functional programming with C++ template metaprograms. In: Horváth, Z., Plasmeijer, R., Zsók, V. (eds.) CEFP 2009. LNCS, vol. 6299, pp. 306–353. Springer, Heidelberg (2010).  https://doi.org/10.1007/978-3-642-17685-2_9CrossRefGoogle Scholar
  31. 31.
    Sinkovics, Á.: Interactive metaprogramming shell based on clang. In: Lecture at C++Now Conference. Aspen, Co., US (2015). https://www.youtube.com/watch?v=oCbeXpJKzlM
  32. 32.
    Sinkovics, Á., Porkoláb, Z.: Expressing C++ template metaprograms as lambda expressions. In: Horváth, Z., Zsók, V., Achten, P., Koopman, P. (eds.) Proceedings of Tenth Symposium on Trends in Functional Programming, Komárno, Slovakia, 2–4 June 2009, pp. 97–111 (2009)Google Scholar
  33. 33.
    Sinkovics, Á., Porkoláb, Z.: Implementing monads for C++ template metaprograms. In: Science of Computer Programming.  https://doi.org/10.1016/j.scico.2013.01.002, http://www.sciencedirect.com/science/article/pii/S0167642313000051. ISSN 0167-6423. Accessed 23 Jan 2013CrossRefGoogle Scholar
  34. 34.
    Sinkovics, Á, Porkoláb, Z.: Domain-specific language integration with C++ template metaprogramming. In: Formal and Practical Aspects of Domain-Specific Languages: Recent Developments, pp. 32–55. IGI Global (2013).  https://doi.org/10.4018/978-1-4666-2092-6.ch002. Accessed 30 Apr 2014
  35. 35.
    Sipos, Á., Zsók, V.: EClean – an embedded functional language. Electron. Not. Theoret. Comput. Sci. 238(2), 47–58 (2009)CrossRefGoogle Scholar
  36. 36.
    Sommerlad, P.: C++14 Compile-time computation (ACCU 2015). http://wiki.hsr.ch/PeterSommerlad/files/ACCU2015VariadicVariableTemplates.pdf
  37. 37.
    Stepanov, A.: From Mathematics to Generic Programming, 1st edn. Addison-Wesley, Boston (2014). ISBN-10: 0321942043, http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3649.html
  38. 38.
    Stroustrup, B.: A history of C++: 1979–1991. In: The Second ACM SIGPLAN Conference on History of Programming Languages (HOPL-II), pp. 271–297. ACM, New York (1996).  https://doi.org/10.1145/154766.155375
  39. 39.
    Stroustrup, B.: The C++ Programming Language, 4th edn. Addison-Wesley Professional, Boston (2013). ISBN-10 0321563840zbMATHGoogle Scholar
  40. 40.
    Stroustrup, B.: The Design and Evolution of C++. Addison-Wesley, Boston (1994)Google Scholar
  41. 41.
    Szűgyi, Z., Sinkovics, Á., Pataki, N., Porkoláb, Z.: C++ metastring library and its applications. In: Fernandes, J.M., Lämmel, R., Visser, J., Saraiva, J. (eds.) GTTSE 2009. LNCS, vol. 6491, pp. 461–480. Springer, Heidelberg (2011).  https://doi.org/10.1007/978-3-642-18023-1_15CrossRefGoogle Scholar
  42. 42.
    Unruh, E.: Prime number computation. ANSI X3J16-94-0075/ISO WG21-462Google Scholar
  43. 43.
    Vali, F., Sutter, H., Abrahams, D.: N3649 Generic (Polymorphic) Lambda Expressions (Revision 3). http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3649.html
  44. 44.
    Veldhuizen, T.: C++ Templates are Turing Complete. Technical report, Indiana University Computer Science (2003). http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.14.3670
  45. 45.
    Veldhuizen, T., Gannon, D.: Active libraries: rethinking the roles of compilers and libraries. In: Proceedings of the SIAM Workshop on Object Oriented Methods for Inter-operable Scientific and Engineering Computing, OO 1998 (1998)Google Scholar
  46. 46.
    Veldhuizen, T.: Expression Templates. C++ Report, vol. 7, pp. 26–31 (1995)Google Scholar
  47. 47.
    Zsók, V., Koopman, P., Plasmeijer, R.: Generic executable semantics for d-clean. Electron. Not. Theoret. Comput. Sci. 279(3), 85–95 (2011)CrossRefGoogle Scholar
  48. 48.
    Järvi, J.: The Boost Lambda library. http://www.boost.org/doc/libs/1_60_0/doc/html/lambda.html
  49. 49.
    Literal types in Draft C++14 standard. Working Draft, Standard for Programming Language C++. ANSI C++ N4290, 19 November 2014. 3.9. [10]. http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4296.pdf
  50. 50.
    Constant expressions in Draft C++14 standard. Working Draft, Standard for Programming Language C++. ANSI C++ N4290, 19 November 2014. 5.20. [4]Google Scholar

Copyright information

© Springer Nature Switzerland AG 2019

Authors and Affiliations

  1. 1.Faculty of Informatics, Department of Programming Languages and CompilersEötvös Loránd UniversityBudapestHungary

Personalised recommendations