Skip to main content

A Semantic Analysis of C++ Templates

  • Conference paper

Part of the Lecture Notes in Computer Science book series (LNCS,volume 4067)

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.

Keywords

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

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.

This is a preview of subscription content, access via your institution.

Buying options

Chapter
USD   29.95
Price excludes VAT (Canada)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD   39.99
Price excludes VAT (Canada)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
Softcover Book
USD   54.99
Price excludes VAT (Canada)
  • 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

Learn about institutional subscriptions

Preview

Unable to display preview. Download preview PDF.

Unable to display preview. Download preview PDF.

References

  1. Haskell 98 Language and Libraries: The Revised Report (December 2002), http://www.haskell.org/onlinereport/index.html

  2. MetaOCaml: A compiled, type-safe multi-stage programming language (2004), Available online from http://www.metaocaml.org/

  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. Alexandrescu, A.: Modern C++ design: generic programming and design patterns applied. Addison-Wesley Longman Publishing Co., Inc., Boston (2001)

    Google Scholar 

  5. Czarnecki, K., Eisenecker, U.W.: Generative programming: methods, tools, and applications. ACM Press/Addison-Wesley Publishing Co., New York (2000)

    Google Scholar 

  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)

    CrossRef  Google Scholar 

  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)

    CrossRef  Google Scholar 

  8. International Organization for Standardization. ISO/ IEC 14882:2003: Programming languages — C++. Geneva, Switzerland (October 2003)

    Google Scholar 

  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. Milner, R., Tofte, M., Harper, R.: The Definition of Standard ML. MIT Press, Cambridge (1990)

    Google Scholar 

  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)

    CrossRef  Google Scholar 

  12. Nipkow, T., Paulson, L.C., Wenzel, M.T.:Isabelle/HOL — A Proof Assistant for Higher-Order Logic. LNCS, vol. 2283. Springer, Heidelberg (2002)

    MATH  Google Scholar 

  13. Reis, G.D., Stroustrup, B.: A formalism for C++. Technical Report N1885=05-0145, ISO/IEC JTC1/SC22/WG21 (2005)

    Google Scholar 

  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)

    CrossRef  Google Scholar 

  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)

    CrossRef  Google Scholar 

  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. Siek, J., Taha, W.: C++.T formalization in Isar. Technical Report TR05-458, Rice University, Houston, TX (December 2005)

    Google Scholar 

  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. Veldhuizen, T.: Using C++ template metaprograms. C++ Report 7(4), 36–43 (1995) Reprinted in C++ Gems, ed. Stanley Lippman.

    Google Scholar 

  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. Wenzel, M.: The Isabelle/Isar Reference Manual. TU München (April 2004)

    Google Scholar 

Download references

Author information

Authors and Affiliations

Authors

Editor information

Editors and Affiliations

Rights and permissions

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

Publish with us

Policies and ethics