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.
Access this chapter
Tax calculation will be finalised at checkout
Purchases are for personal use only
References
Abrahams, D., Gurtovoy, A.: C++ Template Metaprogramming, Concepts, Tools, and Techniques from Boost and Beyond, p. 400. Addison-Wesley, Boston (2004). ISBN 0321-22725-6
Alexandrescu, A.: Modern C++ Design: Generic Programming and Design Patterns Applied. Addison-Wesley, Boston (2001)
Allain, A.: Constexpr - Generalized Constant Expressions in C++11. http://www.cprogramming.com/c++11/c++11-compile-time-processing-with-constexpr.html
Austern, M.H.: Generic Programming and the STL: Using and Extending the C++ Standard Template Library. Addison-Wesley, Boston (1998)
Coplien, J.O.: Multi-Paradigm Design for C++. Addison-Wesley Longman Publishing Co., Inc., Boston (1998)
Czarnecki, K., Eisenecker, U.W.: Generative Programming: Methods, Tools and Applications. Addison-Wesley, Boston (2000)
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 1991
Ellis, M., Stroustrup, B.: The Annotated C++ Reference Manual. Addison-Wesley, Boston (1990)
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-3
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)
Hudak, P.: Conception, evolution, and application of functional programming languages. ACM Comput. Surv. 21(3), 359–411 (1989). https://doi.org/10.1145/72551.72554
The C++11 Standard: ISO International Standard, ISO/IEC 14882:2011(E) - Information technology - Programming languages - C++ (2011)
The C++14 Standard: ISO International Standard, ISO/IEC 14882:2014(E) - Programming Language C++ (2014)
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.504
Järvi, J., Freeman, J.: C++ lambda expressions and closures. Sci. Comput. Program. 75(9), 762–772 (2010)
Karlsson, B.: Beyond the C++ Standard Library, An Introduction to Boost. Addison-Wesley, Boston (2005)
Kernighan, B.W., Ritche, D.M.: The C Programming Language, vol. 2. Prentice-Hall, Englewood Cliffs (1988)
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
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_12
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.: Functional programming in C++ using the FC++ library. SIGPLAN Not. 36(4), 25–30 (2001)
Meyers, S.: Effective STL – 50 Specific Ways to Improve Your Use of the Standard Template Library. Addison-Wesley, Boston (2001)
Meyers, S.: Effective Modern C++. O’Reilly Media, Sebastopol (2014). ISBN 978-1-4919-0399-5, ISBN 10 1-4919-0399-6
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 2011
Milewski, B.: Functional Data Structures in C++. C++Now, Aspen (2015). https://www.youtube.com/watch?v=OsB09djvfl4
Milewski, B.: C++ Ranges are Pure Monadic Goodness. B. Milewski’s blog. https://bartoszmilewski.com/2014/10/17/c-ranges-are-pure-monadic-goodness/
Musser, D.R., Stepanov, A.A.: Algorithm-oriented generic libraries. Softw.-Pract. Exper. 27(7), 623–642 (1994)
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
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)
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_9
Sinkovics, Á.: Interactive metaprogramming shell based on clang. In: Lecture at C++Now Conference. Aspen, Co., US (2015). https://www.youtube.com/watch?v=oCbeXpJKzlM
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)
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 2013
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
Sipos, Á., Zsók, V.: EClean – an embedded functional language. Electron. Not. Theoret. Comput. Sci. 238(2), 47–58 (2009)
Sommerlad, P.: C++14 Compile-time computation (ACCU 2015). http://wiki.hsr.ch/PeterSommerlad/files/ACCU2015VariadicVariableTemplates.pdf
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
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
Stroustrup, B.: The C++ Programming Language, 4th edn. Addison-Wesley Professional, Boston (2013). ISBN-10 0321563840
Stroustrup, B.: The Design and Evolution of C++. Addison-Wesley, Boston (1994)
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_15
Unruh, E.: Prime number computation. ANSI X3J16-94-0075/ISO WG21-462
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
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
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)
Veldhuizen, T.: Expression Templates. C++ Report, vol. 7, pp. 26–31 (1995)
Zsók, V., Koopman, P., Plasmeijer, R.: Generic executable semantics for d-clean. Electron. Not. Theoret. Comput. Sci. 279(3), 85–95 (2011)
Järvi, J.: The Boost Lambda library. http://www.boost.org/doc/libs/1_60_0/doc/html/lambda.html
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
Constant expressions in Draft C++14 standard. Working Draft, Standard for Programming Language C++. ANSI C++ N4290, 19 November 2014. 5.20. [4]
Author information
Authors and Affiliations
Corresponding author
Editor information
Editors and Affiliations
Rights and permissions
Copyright information
© 2019 Springer Nature Switzerland AG
About this chapter
Cite this chapter
Porkoláb, Z. (2019). Immutables in C++: Language Foundation for Functional Programming. In: Zsók, V., Porkoláb, Z., Horváth, Z. (eds) Central European Functional Programming School. CEFP 2015. Lecture Notes in Computer Science(), vol 10094. Springer, Cham. https://doi.org/10.1007/978-3-030-28346-9_3
Download citation
DOI: https://doi.org/10.1007/978-3-030-28346-9_3
Published:
Publisher Name: Springer, Cham
Print ISBN: 978-3-030-28345-2
Online ISBN: 978-3-030-28346-9
eBook Packages: Computer ScienceComputer Science (R0)