C++ Metastring Library and Its Applications

  • Zalán Szűgyi
  • Ábel Sinkovics
  • Norbert Pataki
  • Zoltán Porkoláb
Part of the Lecture Notes in Computer Science book series (LNCS, volume 6491)


C++ template metaprogramming is an emerging direction of generative programming: with proper template definitions we can enforce the C++ compiler to execute algorithms at compilation time. Template metaprograms have become essential part of today’s C++ programs of industrial size; they provide code adoptions, various optimizations, DSL embedding, etc. Besides the compilation time algorithms, template metaprogram data-structures are particularly important. From simple typelists to more advanced STL-like data types there are a variety of such constructs. Interesting enough, until recently string, as one of the most widely used data type of programming, has not been supported. Although, boost::mpl::string is an advance in this area, it still lacks the most fundamental string operations. In this paper, we analysed the possibilities of handling string objects at compilation time with a metastring library. We created a C++ template metaprogram library that provides the common string operations, like creating sub-strings, concatenation, replace, and similar. To provide real-life use-cases we implemented two applications on top of our Metastring library. One use case utilizes compilation time inspection of input in the domain of pattern matching algorithms, thus we are able to select the more optimal search method at compilation time. The other use-case implements safePrint, a type-safe version of printf – a widely investigated problem. We present both the performance improvements and extended functionality we have achieved in the applications of our Metastring library.


Compilation Time Format String Pattern Match Algorithm Partial Evaluator Expression Template 
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.


Unable to display preview. Download preview PDF.

Unable to display preview. Download preview PDF.


  1. 1.
    Alexandrescu, A.: Modern C++ Design: Generic Programming and Design Patterns Applied. Addison-Wesley, Reading (2001)Google Scholar
  2. 2.
    Abrahams, D., Gurtovoy, A.: C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond. Addison-Wesley, Reading (2004)Google Scholar
  3. 3.
    Boyer, R.S., Moore, J.S.: A Fast String Searching Algorithm. Communication of the ACM 20, 762–772 (1977)CrossRefzbMATHGoogle Scholar
  4. 4.
    Cormen, T.H., Leiserson, C.E., Rivest, R.L., Stein, C.: Introduction to Algorithms. The MIT Press, Cambridge (2001)zbMATHGoogle Scholar
  5. 5.
    Czarnecki, K., Eisenecker, U.W.: Generative Programming: Methods, Tools and Applications. Addison-Wesley, Reading (2000)Google Scholar
  6. 6.
    Czarnecki, K., Eisenecker, U.W., Glück, R., Vandevoorde, D., Veldhuizen, T.L.: Generative Programming and Active Libraries. Springer, Heidelberg (2000)CrossRefGoogle Scholar
  7. 7.
    Devadithya, T., Chiu, K., Lu, W.: C++ Reflection for High Performance Problem Solving Environments. In: Proceedings of the 2007 Spring Simulation Multiconference, vol. 2, pp. 435–440 (2007)Google Scholar
  8. 8.
    Futamura, Y.: Partial Evaluation of Computation Process – An approach to a Compiler-Compiler. Systems, Computers, Controls 2(5), 45–50; Reprinted in Higher-Order and Symbolic Computation 12(4), 381–391 (1999), with a foreword. Kluwer Academic Publishers (2000)Google Scholar
  9. 9.
    Gil, J.(Y.), Lenz, K.: Simple and safe SQL queries with c++ templates. In: Proc. of Generative And Component Engineering 2007, The ACM Digital Library, pp. 13–24 (2007)Google Scholar
  10. 10.
    Gregor, D., Järvi, J., Powell, G.: Variadic Templates (Revision 3). Number N2080=06-0150, ANSI/ISO C++ Standard Committee (October 2006)Google Scholar
  11. 11.
    Gregor, D., Järvi, J., Kulkarni, M., Lumsdaine, A., Musser, D., Schupp, S.: Generic programming and high-performance libraries. International Journal of Parallel Programing 33(2), 145–164 (2005)CrossRefGoogle Scholar
  12. 12.
    Jones, N.D.: An introduction to partial evaluation. ACM Computing Surveys 28(3), 480–503 (1996)CrossRefGoogle Scholar
  13. 13.
    Juhász, Z., Sipos, Á., Porkoláb, Z.: Implementation of a Finite State Machine with Active Libraries in C++. In: Lämmel, R., Visser, J., Saraiva, J. (eds.) GTTSE 2007. LNCS, vol. 5235, pp. 474–488. Springer, Heidelberg (2008) ISBN 978-3-540-88642-6Google Scholar
  14. 14.
    Karlsson, B.: Beyond the C++ Standard Library, An Introduction to Boost. Addison-Wesley, Reading (2005)Google Scholar
  15. 15.
    Knuth, D.E., Morris Jr., J.H., Pratt, V.R.: Fast Pattern Matching in Strings. SIAM J. Comput. 6(2), 323–350 (1977)MathSciNetCrossRefzbMATHGoogle Scholar
  16. 16.
    Meyers, S.: Effective STL. Addison-Wesley, Reading (2001)Google Scholar
  17. 17.
    Porkoláb, Z., Mihalicza, J., Sipos, Á.: Debugging C++ Template Metaprograms. In: Proc. of Generative Programming and Component Engineering (GPCE 2006), The ACM Digital Library, pp. 255–264 (2006)Google Scholar
  18. 18.
    ANSI/ISO C++ Committee. Programming Languages – C++. ISO/IEC 14882:1998(E). American National Standards Institute (1998)Google Scholar
  19. 19.
    Siek, J., Lumsdaine, A.: Concept checking: Binding parametric polymorphism in C++. In: First Workshop on C++ Template Metaprogramming (October 2000)Google Scholar
  20. 20.
    Sipos, Á., Porkoláb, Z., Pataki, N., Zsók, V.: Meta < Fun> - Towards a Functional-Style Interface for C++ Template Metaprograms. In: Proceedings of 19th International Symposium of Implementation and Application of Functional Languages (IFL 2007), pp. 489–502 (2007)Google Scholar
  21. 21.
    Stroustrup, B.: Evolving a language in and for the real world: C++ 1991-2006. ACM HOPL-III (June 2007)Google Scholar
  22. 22.
    Stroustrup, B.: The C++ Programming Language Special Edition. Addison- Wesley, Reading (2000)zbMATHGoogle Scholar
  23. 23.
    Unruh, E.: Prime number computation. ANSI X3J16-94-0075/ISO WG21-462Google Scholar
  24. 24.
    Veldhuizen, T.L., Gannon, D.: Active libraries: Rethinking the roles of compilers and libraries. In: Proceedings of the SIAM Workshop on Object Oriented Methods for Interoperable Scientic and Engineering Computing (OO 1998), pp. 21–23. SIAM Press, Philadelphia (1998)Google Scholar
  25. 25.
    Veldhuizen, T.L.: Expression Templates. C++ Report 7(5), 26–31 (1995)Google Scholar
  26. 26.
    Veldhuizen, T.L.: Using C++ Template Metaprograms. C++ Report 7(4), 36–43 (1995)Google Scholar
  27. 27.
    Veldhuizen, T.L.: C++ Templates as Partial Evaluation. In: Danvy, O. (ed.): Proceedings of the 1999 ACM SIGPLAN Workshop on Partial Evaluation and Semantics-Based Program Manipulation, San Antonio, Texas, January 22-23. Technical report BRICS-NS-99-1, University of Aarhus, pp. 13–18 (1999)Google Scholar
  28. 28.
  29. 29.
  30. 30.
    Boost Metaprogramming library,
  31. 31.
    The boost preprocessor metaprogramming library,
  32. 32.
  33. 33.
  34. 34.
  35. 35.
  36. 36.
  37. 37.
  38. 38.

Copyright information

© Springer-Verlag Berlin Heidelberg 2011

Authors and Affiliations

  • Zalán Szűgyi
    • 1
  • Ábel Sinkovics
    • 1
  • Norbert Pataki
    • 1
  • Zoltán Porkoláb
    • 1
  1. 1.Department of Programming Languages and CompilersEötvös Loránd UniversityBudapestHungary

Personalised recommendations