skip to main content
10.1145/1621607.1621613acmconferencesArticle/Chapter ViewAbstractPublication PagesgpceConference Proceedingsconference-collections
research-article

Toward foundations for type-reflective metaprogramming

Published:04 October 2009Publication History

ABSTRACT

C++ template metaprogramming has been used with great success to build software applications and libraries. In practice, however, template metaprogramming suffers usability, reliability, and capability shortcomings, and it is not well understood in theory. Template metaprogramming has these problems because it relies on emergent properties of disparate language features that were tailored to other purposes. As a step toward solid and sound language support for metaprogramming, this paper establishes firm semantic foundations for select capabilities of template metaprogramming.

We analyze C++ and the idioms of template metaprogramming and isolate, in a language-neutral fashion, fundamental capabilities of C++ that enable metaprogramming. Guided by this analysis, we present a design for a core calculus that directly expresses fundamental metaprogramming capabilities, including static computation, code generation, and type reflection. We prove a typesafety property for compile-time evaluation of metaprograms. To formally connect the core calculus to programming practice, we present a more convenient surface language for metaprogramming. Its semantics are captured by type-directed translation to the core calculus. We prove that this translation preserves well-typing.

This idealized presentation averts some of the shortcomings of C++ template metaprogramming and provides a framework for further study.

References

  1. David Abrahams and Ralf W. Grosse-Kunstleve. Building hybrid systems with Boost.Python. C/C++ Users Journal, 21, July 2003.Google ScholarGoogle Scholar
  2. David Abrahams and Aleksey Gurtovoy. C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond. Addison-Wesley Professional, 2004. Google ScholarGoogle ScholarDigital LibraryDigital Library
  3. Andrei Alexandrescu. Modern C++ Design: Generic Programming and Design Patterns Applied. Addison-Wesley, 2001. Google ScholarGoogle ScholarDigital LibraryDigital Library
  4. The Boost C++ library collection. 29 July 2009. http://boost.org.Google ScholarGoogle Scholar
  5. Alonzo Church. A formulation of the simple theory of types. Journal of Symbolic Logic, 5:56--68, 1940.Google ScholarGoogle Scholar
  6. John Clements and Matthias Felleisen. A tail-recursive machine with stack inspection. ACM Transactions on Programming Languages and Systems, 26(6):1029--1052, 2004. Google ScholarGoogle ScholarDigital LibraryDigital Library
  7. Krzysztof Czarnecki and Ulrich W. Eisenecker. Generative programming: Methods, Tools, and Applications. ACM Press/Addison-Wesley Publishing Co., New York, NY, USA, 2000. Google ScholarGoogle ScholarDigital LibraryDigital Library
  8. Joel de Guzman. The Boost Spirit user manual. 29 July 2009. http://boost.org/libs/spirit/.Google ScholarGoogle Scholar
  9. Steven E. Ganz, Amr Sabry, and Walid Taha. Macros as multi-stage computations: Type-safe, generative, binding macros in MacroML. In ICFP '01: Proceedings of the Sixth ACM SIGPLAN International Conference on Functional Programming, pages 74--85, New York, NY, USA, 2001. ACM Press. Google ScholarGoogle ScholarDigital LibraryDigital Library
  10. Joseph (Yossi) Gil and Keren Lenz. Simple and safe SQL queries with C++ templates. In GPCE '07: Proceedings of the Sixth International Conference on Generative Programming and Component Engineering, pages 13--24, New York, NY, USA, 2007. ACM. Google ScholarGoogle ScholarDigital LibraryDigital Library
  11. Shan Shan Huang and Yannis Smaragdakis. Expressive and safe static reflection with MorphJ. In PLDI '08: Proceedings of the 2008 ACM SIGPLAN Conference on Programming Language Design and Implementation, pages 79--89, New York, NY, USA, 2008. ACM. Google ScholarGoogle ScholarDigital LibraryDigital Library
  12. Shan Shan Huang, David Zook, and Yannis Smaragdakis. Morphing: Safely shaping a class in the image of others. In Erik Ernst, editor, ECOOP 2007: Proceedings of the 21st European Conference on Object-Oriented Programming, volume 4609 of Lecture Notes in Computer Science, pages 399--424. Springer, 2007. Google ScholarGoogle ScholarDigital LibraryDigital Library
  13. Jaakko Jarvi, Bjarne Stroustrup, Douglas Gregor, and Jeremy Siek. Decltype and auto. Technical Report N1478=03-0061, ISO/IEC JTC 1, Information Technology, Subcommittee SC 22, : Programming Language C++, 2003.Google ScholarGoogle Scholar
  14. Neil D. Jones, Carsten K. Gomard, and Peter Sestoft. Partial Evaluation and Automatic Program Generation. Prentice-Hall, Inc., Upper Saddle River, NJ, USA, 1993. Google ScholarGoogle ScholarDigital LibraryDigital Library
  15. Benjamin C. Pierce. Types and Programming Languages. MIT Press, Cambridge, MA, 2002. Google ScholarGoogle ScholarDigital LibraryDigital Library
  16. Norman Ramsey, Kathleen Fisher, and Paul Govereau. An expressive language of signatures. In ICFP '05: Proceedings of the Tenth ACM SIGPLAN International Conference on Functional Programming, pages 27--40, New York, NY, USA, 2005. ACM Press. Google ScholarGoogle ScholarDigital LibraryDigital Library
  17. John Reynders. The POOMA framework -- A templated class library for parallel scientific computing. In PPSC '97: Proceedings of the Eighth SIAM Conference on Parallel Processing for Scientific Computing, Minneapolis, Minnesota, USA, March 1997. SIAM (Philadelphia).Google ScholarGoogle Scholar
  18. Tim Sheard and Simon Peyton Jones. Template meta-programming for Haskell. In Haskell '02: Proceedings of the 2002 ACM SIGPLAN Workshop on Haskell, pages 1--16, New York, NY, USA, 2002. ACM. Google ScholarGoogle ScholarDigital LibraryDigital Library
  19. Jeremy G. Siek and Andrew Lumsdaine. The Matrix Template Library: A unifying framework for numerical linear algebra. In Parallel Object Oriented Scientific Computing. ECOOP, 1998. Google ScholarGoogle ScholarDigital LibraryDigital Library
  20. Jeremy G. Siek and Andrew Lumsdaine. Language requirements for large-scale generic libraries. In Robert Gl¨uck and Michael R. Lowry, editors, GPCE '05: Proceedings of the Fourth International Conference on Generative Programming and Component Engineering, volume 3676 of Lecture Notes in Computer Science, pages 405--421. Springer, 2005. Google ScholarGoogle ScholarDigital LibraryDigital Library
  21. Bjarne Stroustrup. Design and Evolution of C++. Addison-Wesley Longman Publishing Co., Inc., Boston, MA, USA, 1994. Google ScholarGoogle ScholarDigital LibraryDigital Library
  22. Walid Taha and Tim Sheard. Multi-stage programming with explicit annotations. In PEPM '97: Proceedings of the 1997 ACM SIGPLAN Symposium on Partial Evaluation and Semantics-based Program Manipulation, pages 203--217, New York, NY, USA, 1997. ACM Press. Google ScholarGoogle ScholarDigital LibraryDigital Library
  23. Todd Veldhuizen. Expression templates. C++ Report, 7(5):26--31, June 1995.Google ScholarGoogle Scholar
  24. Todd Veldhuizen. Using C++ template metaprograms. C++ Report, 7(4):36--43, May 1995. Reprinted in C++ Gems, ed. Stanley Lippman. Google ScholarGoogle ScholarDigital LibraryDigital Library
  25. Todd L. Veldhuizen. Arrays in Blitz++. In Denis Caromel, R. R. Oldehoeft, and Marydell Tholburn, editors, Computing in Object-Oriented Parallel Environments, Second International Symposium, ISCOPE 98, Santa Fe, NM, USA, December 8-11, 1998, Proceedings, volume 1505 of Lecture Notes in Computer Science, pages 223--230. Springer, 1998. Google ScholarGoogle ScholarDigital LibraryDigital Library
  26. Andrew K. Wright and Matthias Felleisen. A syntactic approach to type soundness. Information and Computation, 115(1):38--94, 1994. Google ScholarGoogle ScholarDigital LibraryDigital Library

Index Terms

  1. Toward foundations for type-reflective metaprogramming

      Recommendations

      Comments

      Login options

      Check if you have access through your login credentials or your institution to get full access on this article.

      Sign in
      • Published in

        cover image ACM Conferences
        GPCE '09: Proceedings of the eighth international conference on Generative programming and component engineering
        October 2009
        194 pages
        ISBN:9781605584942
        DOI:10.1145/1621607
        • General Chair:
        • Jeremy Siek,
        • Program Chair:
        • Bernd Fischer
        • cover image ACM SIGPLAN Notices
          ACM SIGPLAN Notices  Volume 45, Issue 2
          GPCE '09
          February 2010
          182 pages
          ISSN:0362-1340
          EISSN:1558-1160
          DOI:10.1145/1837852
          Issue’s Table of Contents

        Copyright © 2009 ACM

        Publisher

        Association for Computing Machinery

        New York, NY, United States

        Publication History

        • Published: 4 October 2009

        Permissions

        Request permissions about this article.

        Request Permissions

        Check for updates

        Qualifiers

        • research-article

        Acceptance Rates

        GPCE '09 Paper Acceptance Rate18of62submissions,29%Overall Acceptance Rate56of180submissions,31%

      PDF Format

      View or Download as a PDF file.

      PDF

      eReader

      View online with eReader.

      eReader
      About Cookies On This Site

      We use cookies to ensure that we give you the best experience on our website.

      Learn more

      Got it!