Skip to main content

Fake Run-Time Selection of Template Arguments in C++

  • Conference paper
Book cover Objects, Models, Components, Patterns (TOOLS 2012)

Part of the book series: Lecture Notes in Computer Science ((LNPSE,volume 7304))

Abstract

C++ does not support run-time resolution of template type arguments. To circumvent this restriction, we can instantiate a template for all possible combinations of type arguments at compile time and then select the proper instance at run time by evaluation of some provided conditions. However, for templates with multiple type parameters such a solution may easily result in a branching code bloat. We present a template metaprogramming algorithm called for_id that allows the user to select the proper template instance at run time with theoretical minimum sustained complexity of the branching code.

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

Access this chapter

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 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

Institutional subscriptions

Preview

Unable to display preview. Download preview PDF.

Unable to display preview. Download preview PDF.

References

  1. Abrahams, D., Gurtovoy, A.: C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond. C++ in Depth Series. Addison-Wesley Professional (2004)

    Google Scholar 

  2. Alexandrescu, A.: Modern C++ Design: Generic Programming and Design Patterns Applied. Addison-Wesley Longman Publishing Co., Inc., Boston (2001)

    Google Scholar 

  3. Balay, S., Brown, J., Buschelman, K., Eijkhout, V., Gropp, W.D., Kaushik, D., Knepley, M.G., McInnes, L.C., Smith, B.F., Zhang, H.: PETSc Users Manual. Tech. Rep. ANL-95/11 - Revision 3.2, Argonne National Laboratory (2010)

    Google Scholar 

  4. Barton, J.J., Nackman, L.R.: Scientific and Engineering C++: An Introduction with Advanced Techniques and Examples, 1st edn. Addison-Wesley Longman Publishing Co., Inc., Boston (1994)

    Google Scholar 

  5. Boisvert, R.F., Pozo, R., Remington, K.: The Matrix Market Exchange Formats: Initial Design. Tech. Rep. NISTIR 5935, National Institute of Standards and Technology (December 1996)

    Google Scholar 

  6. Bourdev, L., Jin, H.: Generic Image Library, http://opensource.adobe.com/gil (accessed December 2011)

  7. Bourdev, L., Järvi, J.: Efficient run-time dispatching in generic programming with minimal code bloat. Science of Computer Programming 76(4), 243–257 (2011)

    Article  Google Scholar 

  8. Davis, T.A., Hu, Y.F.: The University of Florida Sparse Matrix Collection. ACM Transactions on Mathematical Software 38(1) (November 2011)

    Google Scholar 

  9. Dytrych, T., Sviratcheva, K.D., Bahri, C., Draayer, J.P., Vary, J.P.: Dominant role of symplectic symmetry in ab initio no-core shell model results for light nuclei. Physical Review C 76(1), 014315 (2007)

    Article  Google Scholar 

  10. Dytrych, T., Sviratcheva, K.D., Bahri, C., Draayer, J.P., Vary, J.P.: Evidence for symplectic symmetry in ab initio no-core shell model results for light nuclei. Physical Review Letters 98, 162503 (2007)

    Article  Google Scholar 

  11. Dytrych, T., Sviratcheva, K.D., Draayer, J.P., Bahri, C., Vary, J.P.: Ab initio symplectic no-core shell model. Journal of Physics G: Nuclear and Particle Physics 35(12), 123101 (2008)

    Article  Google Scholar 

  12. Gennaro, D.D.: Advanced C++ Metaprogramming. CreateSpace (2011)

    Google Scholar 

  13. Guennebaud, G., Jacob, B., et al.: Eigen, version 3.0.1 (2010), http://eigen.tuxfamily.org (accessed July 2011)

  14. Heroux, M.A., Willenbring, J.M.: Trilinos users guide. Tech. Rep. SAND2003-2952, Sandia National Laboratories (2003)

    Google Scholar 

  15. ISO/IEC 14882:2003: Programming languages: C++ (2003)

    Google Scholar 

  16. OpenFOAM User Guide, Version 2.0.0 (2011)

    Google Scholar 

  17. Prata, S.: Primer Plus, 4th edn. Sams, Indianapolis, IN, USA (2001)

    Google Scholar 

  18. Stroustrup, B.: The C++ Programming Language: Special Edition, 3 edn. Addison-Wesley Professional (February 2000), http://www.worldcat.org/isbn/0201700735

  19. The Boost MPL Library, http://www.boost.org/doc/libs/1_48_0/libs/mpl/doc/index.html (accessed December 12, 2012)

  20. The HDF Group. Hierarchical data format version 5 (2000-2010), http://www.hdfgroup.org/HDF5/ (accessed March 27, 2011)

  21. Vandevoorde, D., Josuttis, N.M.: C++ Templates—The Complete Guide. Addison-Wesley (2002)

    Google Scholar 

  22. Vuduc, R., Demmel, J.W., Yelick, K.A.: OSKI: A library of automatically tuned sparse matrix kernels. Journal of Physics: Conference Series 16(1), 521–530 (2005)

    Article  Google Scholar 

Download references

Author information

Authors and Affiliations

Authors

Editor information

Editors and Affiliations

Rights and permissions

Reprints and permissions

Copyright information

© 2012 Springer-Verlag Berlin Heidelberg

About this paper

Cite this paper

Langr, D., Tvrdík, P., Dytrych, T., Draayer, J.P. (2012). Fake Run-Time Selection of Template Arguments in C++. In: Furia, C.A., Nanz, S. (eds) Objects, Models, Components, Patterns. TOOLS 2012. Lecture Notes in Computer Science, vol 7304. Springer, Berlin, Heidelberg. https://doi.org/10.1007/978-3-642-30561-0_11

Download citation

  • DOI: https://doi.org/10.1007/978-3-642-30561-0_11

  • Publisher Name: Springer, Berlin, Heidelberg

  • Print ISBN: 978-3-642-30560-3

  • Online ISBN: 978-3-642-30561-0

  • eBook Packages: Computer ScienceComputer Science (R0)

Publish with us

Policies and ethics