Skip to main content

DSL in C++ Template Metaprogram

  • Chapter
  • First Online:

Part of the book series: Lecture Notes in Computer Science ((LNTCS,volume 8606))

Abstract

Domain specific language integration has to provide the right balance between the expressive power of the DSL and the implementation and maintenance cost of the applied integration techniques. In this paper we discuss a DSL integration technique for the C++ programming language. The solution is based on compile-time parsing of the DSL code using the C++ template metaprogramming library called Metaparse. The parser generator is the C++ template metaprogram reimplementation of a runtime Haskell parser generator library. The full parsing phase is executed when the host program is compiled. The library uses only standard C++ language features, thus our solution is highly portable. As a demonstration of the power of this approach, we present a highly efficient and type-safe version of printf and the way it can be constructed using our library. Despite the well known syntactical difficulties of C++ template metaprograms, building embedded languages using Metaparse leads to self-documenting maintenable C++ source code.

This is a preview of subscription content, log in via an institution.

Buying options

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

References

  1. Abrahams, D., Gurtovoy, A.: C++ Template Metaprogramming, Concepts, Tools, and Techniques from Boost and Beyond. Addison-Wesley, Boston (2004). p. 400. ISBN-0321-22725-6

    Google Scholar 

  2. Andersson, L.: Parsing with Haskell, 28 October 2001. http://www.cs.lth.se/eda120/assignment4/parser.pdf

  3. Gil, Y., Lenz, K.: Simple and safe SQL queries with C++ templates. In: Consela, C., Lawall, J.L. (eds) 6th International Conference on Generative Programming and Component Engineering, GPCE 2007, Salzburg, Austria, pp. 13–24, 1–3 October 2007

    Google Scholar 

  4. Odersky, M., Spoon, L., Venners, B.: Programming in Scala, 2nd edn. Artima (2010). ISBN-10: 0981531644

    Google Scholar 

  5. The POSIX standard, 1003.1-2008 - IEEE Standard for Information Technology - Portable Operating System Interface (POSIX(R)). http://standards.ieee.org/findstds/standard/1003.1-2008.html

  6. Porkoláb, Z., Sinkovics, Á.: Domain-specific language integration with compile-time parser generator library. In: Visser, E., Järvi, J. (eds) Proceedings of the Ninth International Conference on Generative Programming and Component Engineering, GPCE 2010, Eindhoven, The Netherlands, 10–13 October 2010

    Google Scholar 

  7. Sinkovics, Á., Porkoláb, Z.: Domain-specific language Integration with C++ template metaprogramming. In: Mernik, M. (ed) Formal and Practical Aspects of Domain-Specific Languages: Recent Developments, pp. 33–56. Published by Information Science Reference (an imprint of IGI Global), Hershey. ISBN 978-1-4666-2092-6 (hardcover) - ISBN 978-1-4666-2093-3 (ebook) - ISBN 978-1-4666-2094-0 pp. 33-56

    Google Scholar 

  8. Sinkovics, Á., Porkoláb, Z.: Implementing monads for C++ template metaprograms. In: Science of Computer Programming, Available online 23 January 2013. ISSN 0167–6423, doi:10.1016/j.scico.2013.01.002. (http://www.sciencedirect.com/science/article/pii/S0167642313000051)

  9. Sinkovics, Á., Sajó, E., Porkoláb, Z.: Towards more reliable C++ template metaprograms. In: Penjam, J. (ed.) 12th Symposium on Programming Languages and Software Tools (SPLST 2011), Tallinn, Estonia, pp. 260–271, 5–7 October 2011

    Google Scholar 

  10. Sinkovics, Á., Porkoláb, Z.: Expressing C++ template metaprograms as lambda expressions. In: Horváth, Z., Zsók, V., Achten, P., Koopman, P. (eds.) Proceedings of Tenth Symposium on Trends in Functional Programming, Komrno, Slovakia, pp. 97–111, 2–4 June 2009

    Google Scholar 

  11. Visser, E.: Program transformation with Stratego/XT. In: Lengauer, C., Batory, D., Blum, A., Odersky, M. (eds.) Domain-Specific Program Generation. LNCS, vol. 3016, pp. 216–238. Springer, Heidelberg (2004)

    Chapter  Google Scholar 

  12. The auto specified in C++11. http://en.cppreference.com/w/cpp/language/auto

  13. The Boost. Proto library. http://www.boost.org/doc/libs/1_53_0/doc/html/proto.html

  14. Continuation-passing style on Wikipedia. http://en.wikipedia.org/wiki/Continuation-passing_style

  15. Marlow, S. (ed.): Haskell 2010 Language report (2010). http://www.haskell.org/haskellwiki/Language_and_library_specification

  16. Icon, The Icon Programming Language. http://www.cs.arizona.edu/icon

  17. The ISO C++11 standard, ISO/IEC 14882:2011(E) Information technology - Programming languages - C++. http://www.iso.org/iso/catalogue_detail.htm?csnumber=50372

  18. Katahdin. http://www.chrisseaton.com/katahdin

  19. Milewski, B.: Haskell and C++ template metaprogramming. http://bartoszmilewski.wordpress.com/2009/10/26/haskellc-video-and-slides

  20. Milewski, B., Niebler, E.: Compile-Time/Run-Time Functional Programming in C++. http://2012.cppnow.org/session/variadic-template-metaprogramming-using-monads

  21. The MPL Reference Manual. http://www.boost.org/doc/libs/1_53_0/libs/mpl/doc/refmanual.html

  22. Sinkovics, Á., Porkoláb, Z.: Metaparse - Compile-time parsing with template metaprogramming. http://2012.cppnow.org/session/metaparse-compile-time-parsing-with-template-metaprogramming

  23. The User Manual of mpllibs metaparse. http://abel.web.elte.hu/mpllibs/metaparse/manual.html

  24. Veldhuizen, T., Gannon, D.: Active libraries: rethinking the roles of compilers and libraries. In: Proceedings of the SIAM Workshop on Object Oriented Methods for Inter-Operable Scientific and Engineering Computing OO 1998 (1998)

    Google Scholar 

  25. Veldhuizen, T.: Expression templates. C++ Report 7, 26–31 (1995)

    Google Scholar 

  26. The Stratego Program Transformation Language. http://strategoxt.org/

  27. The Template Haskell programming language. http://www.haskell.org/haskellwiki/Template_Haskell

  28. The xlxpressive library. https://github.com/istvans/mpllibs/tree/master/mpllibs/xlxpressive

  29. The XMF programming language. http://itcentre.tvu.ac.uk/clark/xmf.html

  30. The Boost Xpressive library. http://www.boost.org/doc/libs/1_55_0/doc/html/xpressive.html

  31. Xpressive - “Nested Regexes and Sub-Match Scoping” and “Nested Results”. http://www.boost.org/doc/libs/1_53_0/doc/html/xpressive/user_s_guide.html#boost_xpressive.user_s_guide.grammars_and_nested_matches.nested_regexes_and_sub_match_scoping

Download references

Author information

Authors and Affiliations

Authors

Corresponding author

Correspondence to Zoltán Porkoláb .

Editor information

Editors and Affiliations

A The Grammar

A The Grammar

figure bq
figure br
figure bs
figure bt

Rights and permissions

Reprints and permissions

Copyright information

© 2015 Springer International Publishing Switzerland

About this chapter

Cite this chapter

Porkoláb, Z., Sinkovics, Á., Siroki, I. (2015). DSL in C++ Template Metaprogram. In: Zsók, V., Horváth, Z., Csató, L. (eds) Central European Functional Programming School. CEFP 2013. Lecture Notes in Computer Science(), vol 8606. Springer, Cham. https://doi.org/10.1007/978-3-319-15940-9_3

Download citation

  • DOI: https://doi.org/10.1007/978-3-319-15940-9_3

  • Published:

  • Publisher Name: Springer, Cham

  • Print ISBN: 978-3-319-15939-3

  • Online ISBN: 978-3-319-15940-9

  • eBook Packages: Computer ScienceComputer Science (R0)

Publish with us

Policies and ethics