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.
- David Abrahams and Ralf W. Grosse-Kunstleve. Building hybrid systems with Boost.Python. C/C++ Users Journal, 21, July 2003.Google Scholar
- David Abrahams and Aleksey Gurtovoy. C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond. Addison-Wesley Professional, 2004. Google Scholar
Digital Library
- Andrei Alexandrescu. Modern C++ Design: Generic Programming and Design Patterns Applied. Addison-Wesley, 2001. Google Scholar
Digital Library
- The Boost C++ library collection. 29 July 2009. http://boost.org.Google Scholar
- Alonzo Church. A formulation of the simple theory of types. Journal of Symbolic Logic, 5:56--68, 1940.Google Scholar
- 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 Scholar
Digital Library
- Krzysztof Czarnecki and Ulrich W. Eisenecker. Generative programming: Methods, Tools, and Applications. ACM Press/Addison-Wesley Publishing Co., New York, NY, USA, 2000. Google Scholar
Digital Library
- Joel de Guzman. The Boost Spirit user manual. 29 July 2009. http://boost.org/libs/spirit/.Google Scholar
- 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 Scholar
Digital Library
- 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 Scholar
Digital Library
- 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 Scholar
Digital Library
- 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 Scholar
Digital Library
- 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 Scholar
- 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 Scholar
Digital Library
- Benjamin C. Pierce. Types and Programming Languages. MIT Press, Cambridge, MA, 2002. Google Scholar
Digital Library
- 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 Scholar
Digital Library
- 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 Scholar
- 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 Scholar
Digital Library
- 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 Scholar
Digital Library
- 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 Scholar
Digital Library
- Bjarne Stroustrup. Design and Evolution of C++. Addison-Wesley Longman Publishing Co., Inc., Boston, MA, USA, 1994. Google Scholar
Digital Library
- 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 Scholar
Digital Library
- Todd Veldhuizen. Expression templates. C++ Report, 7(5):26--31, June 1995.Google Scholar
- Todd Veldhuizen. Using C++ template metaprograms. C++ Report, 7(4):36--43, May 1995. Reprinted in C++ Gems, ed. Stanley Lippman. Google Scholar
Digital Library
- 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 Scholar
Digital Library
- Andrew K. Wright and Matthias Felleisen. A syntactic approach to type soundness. Information and Computation, 115(1):38--94, 1994. Google Scholar
Digital Library
Index Terms
Toward foundations for type-reflective metaprogramming
Recommendations
A Survey of Metaprogramming Languages
Metaprogramming is the process of writing computer programs that treat programs as data, enabling them to analyze or transform existing programs or generate new ones. While the concept of metaprogramming has existed for several decades, activities ...
Toward foundations for type-reflective metaprogramming
GPCE '09C++ 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 ...
Explicitly heterogeneous metaprogramming with MetaHaskell
ICFP '12: Proceedings of the 17th ACM SIGPLAN international conference on Functional programmingLanguages with support for metaprogramming, like MetaOCaml, offer a principled approach to code generation by guaranteeing that well-typed metaprograms produce well-typed programs. However, many problem domains where metaprogramming can fruitfully be ...







Comments