A Semantic Analysis of C++ Templates

  • Jeremy Siek
  • Walid Taha
Part of the Lecture Notes in Computer Science book series (LNCS, volume 4067)


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.


Type Evaluation Abstract Syntax Type Check Typing Judgment Member Function 
These keywords were added by machine and not by the authors. This process is experimental and the keywords may be updated as the learning algorithm improves.


Unable to display preview. Download preview PDF.

Unable to display preview. Download preview PDF.


  1. 1.
    Haskell 98 Language and Libraries: The Revised Report (December 2002),
  2. 2.
    MetaOCaml: A compiled, type-safe multi-stage programming language (2004), Available online from
  3. 3.
    Abrahams, D., Gurtovoy, A.: C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond. Addison-Wesley Longman Publishing Co., Inc., Boston (2004)Google Scholar
  4. 4.
    Alexandrescu, A.: Modern C++ design: generic programming and design patterns applied. Addison-Wesley Longman Publishing Co., Inc., Boston (2001)Google Scholar
  5. 5.
    Czarnecki, K., Eisenecker, U.W.: Generative programming: methods, tools, and applications. ACM Press/Addison-Wesley Publishing Co., New York (2000)Google Scholar
  6. 6.
    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)CrossRefGoogle Scholar
  7. 7.
    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)CrossRefGoogle Scholar
  8. 8.
    International Organization for Standardization. ISO/ IEC 14882:2003: Programming languages — C++. Geneva, Switzerland (October 2003)Google Scholar
  9. 9.
    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)Google Scholar
  10. 10.
    Milner, R., Tofte, M., Harper, R.: The Definition of Standard ML. MIT Press, Cambridge (1990)Google Scholar
  11. 11.
    Nipkow, T.: Structured Proofs in Isar/HOL. In: Geuvers, H., Wiedijk, F. (eds.) TYPES 2002. LNCS, vol. 2646, pp. 259–278. Springer, Heidelberg (2003)CrossRefGoogle Scholar
  12. 12.
    Nipkow, T., Paulson, L.C., Wenzel, M.T.:Isabelle/HOL — A Proof Assistant for Higher-Order Logic. LNCS, vol. 2283. Springer, Heidelberg (2002)MATHGoogle Scholar
  13. 13.
    Reis, G.D., Stroustrup, B.: A formalism for C++. Technical Report N1885=05-0145, ISO/IEC JTC1/SC22/WG21 (2005)Google Scholar
  14. 14.
    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)CrossRefGoogle Scholar
  15. 15.
    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)CrossRefGoogle Scholar
  16. 16.
    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)Google Scholar
  17. 17.
    Siek, J., Taha, W.: C++.T formalization in Isar. Technical Report TR05-458, Rice University, Houston, TX (December 2005)Google Scholar
  18. 18.
    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)Google Scholar
  19. 19.
    Veldhuizen, T.: Using C++ template metaprograms. C++ Report 7(4), 36–43 (1995) Reprinted in C++ Gems, ed. Stanley Lippman.Google Scholar
  20. 20.
    Wallace, C.: The semantics of the C++ programming language. In: Specification and validation methods, pp. 131–164. Oxford University Press, Inc., New York (1995)Google Scholar
  21. 21.
    Wenzel, M.: The Isabelle/Isar Reference Manual. TU München (April 2004)Google Scholar

Copyright information

© Springer-Verlag Berlin Heidelberg 2006

Authors and Affiliations

  • Jeremy Siek
    • 1
  • Walid Taha
    • 1
  1. 1.Rice UniversityHoustonUSA

Personalised recommendations