skip to main content
10.1145/1244002.1244243acmconferencesArticle/Chapter ViewAbstractPublication PagessacConference Proceedingsconference-collections
Article

Variadic templates for C++

Published:11 March 2007Publication History

ABSTRACT

Generic functions and classes accepting a variable number of type arguments have proven to be a very useful, but missing, feature of C++. Numerous foundational libraries rely on clever template and preprocessor tricks to emulate such variadic templates. By several measures these emulations are inadequate. This paper describes how C++ can be extended with variadic templates, significantly improving existing implementations of widely used C++ libraries in terms of code size, quality of error diagnostics, compilation speed, and generality. Furthermore, variadic templates enable new applications, such as type-safe implementations of functions like printf, and improved support for generic mixin classes. We have implemented variadic templates as an extension of a production quality C++ compiler.

References

  1. Peter Dimov. The Boost Bind library. http://www.boost.org/libs/bind/bind.html, August 2001.Google ScholarGoogle Scholar
  2. Jaakko Järvi, Gary Powell, and Andrew Lumsdaine. The Lambda Library: unnamed functions in C++ Software---Practice and Experience, 33:259--291, 2003. Google ScholarGoogle ScholarDigital LibraryDigital Library
  3. Jaakko Järvi. Tuple types and multiple return values. C/C++ Users Journal, 19:24--35, August 2001. Google ScholarGoogle ScholarDigital LibraryDigital Library
  4. Dan Marsden Joel de Guzman. Boost Fusion. http://spirit.sourceforge.net/dl_more/fusion_v2/libs/fusion/doc/html/index.html, 2006.Google ScholarGoogle Scholar
  5. Douglas Gregor. Boost.Function. Boost. http://www.boost.org/doc/html/function.html.Google ScholarGoogle Scholar
  6. Aleksei Gurtovoy and David Abrahams. The Boost C++ metaprogramming library. www.boost.org/libs/mp1, 2002.Google ScholarGoogle Scholar
  7. Pete Becker. Working draft, standard for programming language C++. Technical Report N2009=06-0079, ISO/IEC JTC 1, Information Technology, Subcommittee SC 22, Programming Language C++, April 2006.Google ScholarGoogle Scholar
  8. Douglas Gregor. Variadic templates for GCC. http://www.osl.iu.edu/~dgregor/cpp/variadic-templates.html, August 2006.Google ScholarGoogle Scholar
  9. Vesa Karvonen and Paul Mensonides. The Boost Preprocessor library, http://www.boost.org/libs/preprocessor/doc/index.html, July 2001.Google ScholarGoogle Scholar
  10. Douglas Gregor. {libstdc++ patch} trl::bind, take 2. GNU libstdc++ mailing list, March 2005. http://gcc.gnu.org/ml/libstdc++/2005--03/msg00367.html.Google ScholarGoogle Scholar
  11. Yannis Smaragdakis and Don S. Batory. Mixin-based programming in C++. In GCSE '00: Proceedings of the Second International Symposium on Generative and Component-Based Software Engineering, pages 163--177, London, UK, 2001. Springer-Verlag. Google ScholarGoogle ScholarDigital LibraryDigital Library
  12. Howard E. Hinnant, Dave Abrahams, and Peter Dimov. A proposal to add an rvalue reference to the C++ language. Technical Report N1690=04-0130, ISO/IEC JTC 1, Information technology, Subcommittee SC 22, Programming language C++, September 2004. http://www.open-std.org/jtcl/sc22/wg21/docs/papers/2004/n1690.html.Google ScholarGoogle Scholar
  13. Joel de Guzman. The Spirit C++ parser framework. http://spirit.sourceforge.net, 2006.Google ScholarGoogle Scholar
  14. Andrei Alexandrescu. Modern C++ Design: Generic Programming and Design Patterns Applied. Addison-Wesley, 2001. Google ScholarGoogle ScholarDigital LibraryDigital Library
  15. Todd L. Veldhuizen. C++ templates are Turing complete, www.osl.iu.edu/~tveldhui/papers/2003/turing.pdf, 2003.Google ScholarGoogle Scholar
  16. Matt Austern. (Draft) Technical Report on Standard Library Extensions. Number N1660=04-0100 in ISO C++ Standard Committee 2004--07 mailing, 2004.Google ScholarGoogle Scholar
  17. James Gosling, Bill Joy, Guy Steele, and Gilad Bracha. The Java Language Specification, Third Edition. Addison-Wesley Longman Publishing Co., Inc., Boston, MA, USA, 2005. Google ScholarGoogle ScholarDigital LibraryDigital Library
  18. ECMA. C# Language Specification, June 2005. http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-334.pdf.Google ScholarGoogle Scholar
  19. David A. Hall. Variable number of generic params. Java bug report 6261297: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6261279, 2005.Google ScholarGoogle Scholar
  20. Walter Bright. D programming language. http://www.digitalmars.com/d/, 2006.Google ScholarGoogle Scholar
  21. Olivier Danvy. Functional unparsing. Journal of Functional Programming, 8(6):621--625, 1998. Google ScholarGoogle ScholarDigital LibraryDigital Library
  22. Tim Sheard and Simon Peyton Jones. Template meta-programming for haskell. SIGPLAN Not., 37(12):60--75, 2002. Google ScholarGoogle ScholarDigital LibraryDigital Library
  23. Bruno C. d. S. Oliveira and Jeremy Gibbons. Typecase: a design pattern for type-indexed functions. In Haskell '05: Proceedings of the 2005 ACM SIGPLAN workshop on Haskell, pages 98--109, New York, NY, USA, 2005. ACM Press. Google ScholarGoogle ScholarDigital LibraryDigital Library
  24. Samuel Krempp. The Boost Formatambda library. http://www.boost.org/libs/format, 2002.Google ScholarGoogle Scholar
  25. H. Abelson, R. K. Dybvig, C. T. Haynes, G. J. Rozas, N. I. Adams Iv, D. P. Friedman, E. Kohlbecker, Jr. G. L. Steele, D. H. Bartley, R. Halstead, D. Oxley, G. J. Sussman, G. Brooks, C. Hanson, K. M. Pitman, and M. Wand. Revised5 report on the algorithmic language Scheme. Higher-Order and Symbolic Computation, 11(1):7--105, 1998. Google ScholarGoogle ScholarDigital LibraryDigital Library
  26. Douglas Gregor, Gary Powell, and Jaakko Järvi. Typesafe variable-length function and template argument lists. Number N1483=03-0066 in ISO C++ Standard Committee Post-Oxford mailing, April 2003.Google ScholarGoogle Scholar
  27. Douglas Gregor, Jaakko Järvi, and Gary Powell. Variadic templates. Number N1603=04-0043 in ISO C++ Standard Committee Pre-Sydney mailing, February 2004.Google ScholarGoogle Scholar
  28. Douglas Gregor, Jaakko Järvi, and Gary Powell. Variadic templates: Exploring the design space. Number N1704=04-0144 in ISO C++ Standard Committee Pre-Redmond mailing, September 2004.Google ScholarGoogle Scholar

Index Terms

  1. Variadic templates for C++

    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
      SAC '07: Proceedings of the 2007 ACM symposium on Applied computing
      March 2007
      1688 pages
      ISBN:1595934804
      DOI:10.1145/1244002

      Copyright © 2007 ACM

      Permission to make digital or hard copies of all or part of this work for personal or classroom use is granted without fee provided that copies are not made or distributed for profit or commercial advantage and that copies bear this notice and the full citation on the first page. Copyrights for components of this work owned by others than ACM must be honored. Abstracting with credit is permitted. To copy otherwise, or republish, to post on servers or to redistribute to lists, requires prior specific permission and/or a fee. Request permissions from [email protected]

      Publisher

      Association for Computing Machinery

      New York, NY, United States

      Publication History

      • Published: 11 March 2007

      Permissions

      Request permissions about this article.

      Request Permissions

      Check for updates

      Qualifiers

      • Article

      Acceptance Rates

      Overall Acceptance Rate1,650of6,669submissions,25%

    PDF Format

    View or Download as a PDF file.

    PDF

    eReader

    View online with eReader.

    eReader