Abstract
Templates are a powerful but poorly understood feature of the C++ language. Their syntax resembles the parameterized classes of other languages (e.g., of Java). But because C++ supports template specialization, their semantics is quite different from that of parameterized classes. Template specialization provides a Turing-complete sub-language within C++ that executes at compile-time. Programmers put this power to many uses. For example, templates are a popular tool for writing program generators.
The C++ Standard defines the semantics of templates using natural language, so it is prone to misinterpretation. The meta-theoretic properties of C++ templates have not been studied, so the semantics of templates has not been systematically checked for errors. In this paper we present the first formal account of C++ templates including some of the more complex aspects, such as template partial specialization. We validate our semantics by proving type safety and verify the proof with the Isabelle proof assistant. Our formalization reveals two interesting issues in the C++ Standard: the first is a problem with member instantiation and the second concerns the generation of unnecessary template specializations.
This work was supported by NSF ITR-0113569 Putting Multi-Stage Annotations to Work, Texas ATP 003604-0032-2003 Advanced Languages Techniques for Device Drivers, and NSF SOD-0439017 Synthesizing Device Drivers.
Access this chapter
Tax calculation will be finalised at checkout
Purchases are for personal use only
Preview
Unable to display preview. Download preview PDF.
Similar content being viewed by others
References
Haskell 98 Language and Libraries: The Revised Report (December 2002), http://www.haskell.org/onlinereport/index.html
MetaOCaml: A compiled, type-safe multi-stage programming language (2004), Available online from http://www.metaocaml.org/
Abrahams, D., Gurtovoy, A.: C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond. Addison-Wesley Longman Publishing Co., Inc., Boston (2004)
Alexandrescu, A.: Modern C++ design: generic programming and design patterns applied. Addison-Wesley Longman Publishing Co., Inc., Boston (2001)
Czarnecki, K., Eisenecker, U.W.: Generative programming: methods, tools, and applications. ACM Press/Addison-Wesley Publishing Co., New York (2000)
Ernst, E., Ostermann, K., Cook, W.R.: A virtual class calculus. In: POPL 2006: Conference record of the 33rd ACM SIGPLAN-SIGACT symposium on Principles of programming languages, pp. 270–282. ACM Press, New York (2006)
Ganz, S.E., Sabry, A., Taha, W.: Macros as multi-stage computations: type-safe, generative, binding macros in MacroML. In: ICFP 2001: Proceedings of the sixth ACM SIGPLAN international conference on Functional programming, pp. 74–85. ACM Press, New York (2001)
International Organization for Standardization. ISO/ IEC 14882:2003: Programming languages — C++. Geneva, Switzerland (October 2003)
Leroy, X.: The Objective Caml system: Documentation and user’s manual, 2000. With Damien Doligez, Jacques Garrigue, Didier Rémy, and Jérôme Vouillon (2000)
Milner, R., Tofte, M., Harper, R.: The Definition of Standard ML. MIT Press, Cambridge (1990)
Nipkow, T.: Structured Proofs in Isar/HOL. In: Geuvers, H., Wiedijk, F. (eds.) TYPES 2002. LNCS, vol. 2646, pp. 259–278. Springer, Heidelberg (2003)
Nipkow, T., Paulson, L.C., Wenzel, M.T.:Isabelle/HOL — A Proof Assistant for Higher-Order Logic. LNCS, vol. 2283. Springer, Heidelberg (2002)
Reis, G.D., Stroustrup, B.: A formalism for C++. Technical Report N1885=05-0145, ISO/IEC JTC1/SC22/WG21 (2005)
Dos Reis, G., Stroustrup, B.: Specifying c++ concepts. In: POPL ’06: Conference record of the 33rd ACM SIGPLAN-SIGACT symposium on Principles of programming languages, pp. 295–308. ACM Press, New York (2006)
Sheard, T., Jones, S.P.: Template meta-programming for haskell. In: Haskell 2002: Proceedings of the 2002 ACM SIGPLAN workshop on Haskell, pp. 1–16. ACM Press, New York (2002)
Siek, J., Gregor, D., Garcia, R., Willcock, J., Järvi, J., Lumsdaine, A.: Concepts for C++0x. Technical Report N1758=05-0018, ISO/IEC JTC 1, Information Technology, Subcommittee SC 22, Programming Language C++ (January 2005)
Siek, J., Taha, W.: C++.T formalization in Isar. Technical Report TR05-458, Rice University, Houston, TX (December 2005)
Vandevoorde, D.: Reflective metaprogramming in C++. Technical Report N1471/03-0054, ISO/IEC JTC 1, Information Technology, Subcommittee SC 22, Programming Language C++ (April 2003)
Veldhuizen, T.: Using C++ template metaprograms. C++ Report 7(4), 36–43 (1995) Reprinted in C++ Gems, ed. Stanley Lippman.
Wallace, C.: The semantics of the C++ programming language. In: Specification and validation methods, pp. 131–164. Oxford University Press, Inc., New York (1995)
Wenzel, M.: The Isabelle/Isar Reference Manual. TU München (April 2004)
Author information
Authors and Affiliations
Editor information
Editors and Affiliations
Rights and permissions
Copyright information
© 2006 Springer-Verlag Berlin Heidelberg
About this paper
Cite this paper
Siek, J., Taha, W. (2006). A Semantic Analysis of C++ Templates. In: Thomas, D. (eds) ECOOP 2006 – Object-Oriented Programming. ECOOP 2006. Lecture Notes in Computer Science, vol 4067. Springer, Berlin, Heidelberg. https://doi.org/10.1007/11785477_19
Download citation
DOI: https://doi.org/10.1007/11785477_19
Publisher Name: Springer, Berlin, Heidelberg
Print ISBN: 978-3-540-35726-1
Online ISBN: 978-3-540-35727-8
eBook Packages: Computer ScienceComputer Science (R0)