skip to main content
research-article

Open and efficient type switch for C++

Published:19 October 2012Publication History
Skip Abstract Section

Abstract

Selecting operations based on the run-time type of an object is key to many object-oriented and functional programming techniques. We present a technique for implementing open and efficient type switching on hierarchical extensible data types. The technique is general and copes well with C++ multiple inheritance. To simplify experimentation and gain realistic performance using production-quality compilers and tool chains, we implement a type switch construct as an ISO C++11 library, called Mach7. This library-only implementation provides concise notation and outperforms the visitor design pattern, commonly used for case analysis on types in object-oriented programming. For closed sets of types, its performance roughly equals equivalent code in functional languages, such as OCaml and Haskell. The type-switching code is easier to use and is more expressive than hand-coded visitors are. The library is non-intrusive and circumvents most of the extensibility restrictions typical of the visitor design pattern. It was motivated by applications involving large, typed, abstract syntax trees.

Skip Supplemental Material Section

Supplemental Material

References

  1. Clang: a C language family frontend for LLVM. http://clang.llvm.org/, 2007.Google ScholarGoogle Scholar
  2. Liz: A System for Axiomatic Programming. http://liz.axiomatics.org/, 2012.Google ScholarGoogle Scholar
  3. A. Appel, L. Cardelli, K. Fisher, C. Gunter, R. Harper, X. Leroy, M. Lillibridge, D. B. MacQueen, J. Mitchell, G. Morrisett, J. H. Reppy, J. G. Riecke, Z. Shao, and C. A. Stone. Principles and a preliminary design for ML2000. March 1999.Google ScholarGoogle Scholar
  4. L. Augustsson. Compiling pattern matching. In Proc. of a conference on Functional programming languages and computer architecture, pages 368--381, New York, NY, USA, 1985. Springer-Verlag New York, Inc. Google ScholarGoogle ScholarDigital LibraryDigital Library
  5. L. Cardelli. Compiling a functional language. In Proc. of the 1984 ACM Symposium on LISP and functional programming, LFP '84, pages 208--217, New York, NY, USA, 1984. ACM. Google ScholarGoogle ScholarDigital LibraryDigital Library
  6. L. Cardelli, J. Donahue, M. Jordan, B. Kalsow, and G. Nelson. The Modula-3 type system. In Proc. of the 16th ACM SIGPLAN-SIGACT symposium on Principles of programming languages, POPL '89, pages 202--212, New York, NY, USA, 1989. ACM. Google ScholarGoogle ScholarDigital LibraryDigital Library
  7. Y. Caseau. Efficient handling of multiple inheritance hierarchies. In Proc. of the 8th conference on Object-oriented programming systems, languages, and applications, OOPSLA '93, pages 271--287, New York, NY, USA, 1993. ACM. Google ScholarGoogle ScholarDigital LibraryDigital Library
  8. CodeSourcery, Compaq, EDG, HP, IBM, Intel, Red Hat, and SGI. Itanium C++ ABI, March 2001. http://www.codesourcery.com/public/cxx-abi/.Google ScholarGoogle Scholar
  9. N. H. Cohen. Type-extension type test can be performed in constant time. ACM Trans. Program. Lang. Syst., 13(4):626--629, Oct. 1991. Google ScholarGoogle ScholarDigital LibraryDigital Library
  10. W. R. Cook. Object-oriented programming versus abstract data types. In Proc. of the REX School/Workshop on Foundations of Object-Oriented Languages, pages 151--178, London, UK, 1991. Springer-Verlag. Google ScholarGoogle ScholarDigital LibraryDigital Library
  11. O.-J. Dahl. SIMULA 67 common base language, (Norwegian Computing Center. Publication). 1968. ISBN B0007JZ9J6. Google ScholarGoogle ScholarDigital LibraryDigital Library
  12. J. Dean, G. DeFouw, D. Grove, V. Litvinov, and C. Chambers. Vortex: an optimizing compiler for object-oriented languages. In Proc. of the 11th ACM SIGPLAN conference on Object-oriented programming, systems, languages, and applications, OOPSLA '96, pages 83--100, New York, NY, USA, 1996. Google ScholarGoogle ScholarDigital LibraryDigital Library
  13. L. P. Deutsch and A. M. Schiffman. Efficient implementation of the smalltalk-80 system. In Proc. of the 11th ACM SIGACT-SIGPLAN symposium on Principles of programming languages, POPL '84, pages 297--302, New York, NY, USA, 1984. ACM. Google ScholarGoogle ScholarDigital LibraryDigital Library
  14. E. W. Dijkstra. Guarded commands, non-determinacy and formal derivation of programs. Jan. 1975.Google ScholarGoogle Scholar
  15. G. Dos Reis and B. Stroustrup. A principled, complete, and efficient representation of C++. In Proc. Joint Conference of ASCM 2009 and MACIS 2009, volume 22 of COE Lecture Notes, pages 407--421, December 2009.Google ScholarGoogle Scholar
  16. K. Driesen and U. Holzle. The direct cost of virtual function calls in C++. In Proc. of the 11th ACM SIGPLAN conference on Object-oriented programming, systems, languages, and applications, OOPSLA '96, pages 306--323, New York, NY, USA, 1996. ACM. Google ScholarGoogle ScholarDigital LibraryDigital Library
  17. R. Ducournau. Perfect hashing as an almost perfect subtype test. ACM Trans. Program. Lang. Syst., 30(6):33:1--33:56, Oct. 2008. ISSN 0164-0925. Google ScholarGoogle ScholarDigital LibraryDigital Library
  18. M. A. Ellis and B. Stroustrup. The annotated C++ reference manual. Addison-Wesley Longman Publishing Co., Inc., Boston, MA, USA, 1990. ISBN 0-201-51459-1. Google ScholarGoogle ScholarDigital LibraryDigital Library
  19. B. Emir. Object-oriented pattern matching. PhD thesis, Lausanne, 2007.Google ScholarGoogle Scholar
  20. M. D. Ernst, C. S. Kaplan, and C. Chambers. Predicate dispatching: A unified theory of dispatch. In ECOOP '98, the 12th European Conference on Object-Oriented Programming, pages 186--211, Brussels, Belgium, July 20-24, 1998. Google ScholarGoogle ScholarDigital LibraryDigital Library
  21. E. Gamma, R. Helm, R. E. Johnson, and J. M. Vlissides. Design patterns: Abstraction and reuse of object-oriented design. In Proc. of the 7th European Conference on Object-Oriented Programming, ECOOP '93, pages 406-431, London, UK, UK, 1993. Springer-Verlag. Google ScholarGoogle ScholarDigital LibraryDigital Library
  22. J. Garrigue. Programmingwith polymorphic variants. In ACM Workshop on ML, Sept. 1998.Google ScholarGoogle Scholar
  23. M. Gibbs and B. Stroustrup. Fast dynamic casting. Softw. Pract. Exper., 36:139--156, February 2006. Google ScholarGoogle ScholarDigital LibraryDigital Library
  24. N. Glew. Type dispatch for named hierarchical types. In Proc. of the 4th ACM SIGPLAN international conference on Func-tional programming, ICFP '99, pages 172--182, New York, NY, USA, 1999. Google ScholarGoogle ScholarDigital LibraryDigital Library
  25. C. Grothoff. Walkabout revisited: The runabout. In ECOOP 2003 - Object-Oriented Programming, pages 103--125. Springer-Verlag, 2003.Google ScholarGoogle ScholarCross RefCross Ref
  26. R. Harper and G. Morrisett. Compiling polymorphism using intensional type analysis. In Proc. of the 22nd ACM SIGPLAN-SIGACT symposium on Principles of programming languages, POPL '95, pages 130--141, New York, NY, USA, 1995. ACM. Google ScholarGoogle ScholarDigital LibraryDigital Library
  27. U. Holzle, C. Chambers, and D. Ungar. Optimizing dynamically-typed object-oriented languages with polymorphic inline caches. In Proc. of the European Conference on Object-Oriented Programming, volume 512 of Lecture Notes in Computer Science, pages 21-38, Berlin, Germany, 1991. Springer-Verlag. Google ScholarGoogle ScholarDigital LibraryDigital Library
  28. International Organization for Standardization. ISO/IEC 14882:2011: Programming languages: C++. Geneva, Switzerland, 3rd edition, 2011.Google ScholarGoogle Scholar
  29. S. P. Jones, editor. Haskell 98 Language and Libraries - The Revised Report. Cambridge University Press, Cambridge, England, 2003.Google ScholarGoogle Scholar
  30. A. Kennedy and C. V. Russo. Generalized algebraic data types and object-oriented programming. In Proc. of the 20th annual ACM SIGPLAN conference on Object-oriented programming, systems, languages, and applications, OOPSLA '05, pages 21--40, New York, NY, USA, 2005. ACM. Google ScholarGoogle ScholarDigital LibraryDigital Library
  31. A. Krall, J. Vitek, and R. N. Horspool. Near optimal hierarchical encoding of types. In In Proc. European Conference on Object-Oriented Programming, ECOOP'97, Lecture Notes in Computer Science, pages 128--145. Springer-Verlag, 1997.Google ScholarGoogle ScholarCross RefCross Ref
  32. S. Krishnamurthi, M. Felleisen, and D. Friedman. Synthesizing object-oriented and functional design to promote reuse. In E. Jul, editor, ECOOP'98 - Object-Oriented Programming, volume 1445 of Lecture Notes in Computer Science, pages 91--113. Springer Berlin / Heidelberg, 1998. Google ScholarGoogle ScholarDigital LibraryDigital Library
  33. B. Liskov. Keynote address - data abstraction and hierarchy. In OOPSLA '87: Addendum to the proc. on Object-oriented programming systems, languages and applications, pages 17--34, New York, NY, USA, 1987. ACM Press. Google ScholarGoogle ScholarDigital LibraryDigital Library
  34. B. Liskov, R. R. Atkinson, T. Bloom, E. B. Moss, R. Schaffert, and A. Snyder. Clu reference manual. Technical report, Cambridge, MA, USA, 1979. Google ScholarGoogle ScholarDigital LibraryDigital Library
  35. A. Loh and R. Hinze. Open data types and open functions. In Proc. of the 8th ACM SIGPLAN international conference on Principles and practice of declarative programming, PPDP '06, pages 133--144, New York, NY, USA, 2006. ACM. Google ScholarGoogle ScholarDigital LibraryDigital Library
  36. Microsoft Research. Phoenix compiler and shared source common language infrastructure. http://research.microsoft.com/phoenix/, 2005.Google ScholarGoogle Scholar
  37. R. Milner, M. Tofte, and R. Harper. The Definition of Standard ML. MIT Press, Cambridge, MA, USA, 1990. Google ScholarGoogle ScholarDigital LibraryDigital Library
  38. P.-E. Moreau, C. Ringeissen, and M. Vittek. A pattern matching compiler for multiple target languages. In Proc. of the 12th international conference on Compiler construction, CC'03, pages 61--76, Berlin, Heidelberg, 2003. Springer-Verlag. Google ScholarGoogle ScholarDigital LibraryDigital Library
  39. M. Odersky, V. Cremet, I. Dragos, G. Dubochet, B. Emir, S. Mcdirmid, S. Micheloud, N. Mihaylov, M. Schinz, E. Stenman, L. Spoon, andM. Zenger. An overview of the Scala programming language (2nd edition). Technical Report LAMP-REPORT-2006-001, Ecole Polytechnique Federale de Lausanne, 2006.Google ScholarGoogle Scholar
  40. B. C. Oliveira, M. Wang, and J. Gibbons. The visitor pattern as a reusable, generic, type-safe component. In Proc. of the 23rd ACM SIGPLAN conference on Object-oriented programming systems languages and applications, OOPSLA '08, pages 439--456, New York, NY, USA, 2008. ACM. Google ScholarGoogle ScholarDigital LibraryDigital Library
  41. J. Palsberg and C. B. Jay. The essence of the visitor pattern. In Proc. of the 22nd International Computer Software and Applications Conference, COMPSAC '98, pages 9-15,Washing-ton, DC, USA, 1998. IEEE Computer Society. Google ScholarGoogle ScholarDigital LibraryDigital Library
  42. S. Peyton Jones, D. Vytiniotis, S. Weirich, and G. Washburn. Simple unification-based type inference for GADTs. In Proc. of the 11th ACM SIGPLAN international conference on Functional programming, ICFP '06, pages 50-61, New York, NY, USA, 2006. ACM. Google ScholarGoogle ScholarDigital LibraryDigital Library
  43. T. Ramananandro, G. Dos Reis, and X. Leroy. Formal verification of object layout for C++ multiple inheritance. In Proc. of the 38th annual ACM SIGPLAN-SIGACT symposium on Principles of programming languages, POPL '11, pages 67--80, New York, NY, USA, 2011. ACM. Google ScholarGoogle ScholarDigital LibraryDigital Library
  44. J. G. Rossie, Jr. and D. P. Friedman. An algebraic semantics of subobjects. In Proc. of the 10th conference on Object-oriented programming systems, languages, and applications, OOPSLA '95, pages 187--199, New York, NY, USA, 1995. ACM. Google ScholarGoogle ScholarDigital LibraryDigital Library
  45. L. Schubert, M. Papalaskaris, and J. Taugher. Determining type, part, color, and time relationships. Computer, 16:53--60, 1983. ISSN 0018-9162. Google ScholarGoogle ScholarDigital LibraryDigital Library
  46. D. A. Spuler. Compiler CodeGeneration for Multiway Branch Statements as a Static Search Problem. Technical Report Technical Report 94/03, James Cook University, Jan. 1994.Google ScholarGoogle Scholar
  47. B. Stroustrup. Multiple inheritance for C++. In Proc. of the Spring'87 EUUG Conference, EUUG '87, May 1987. Revised version in AT&T C++ Translator Release Notes, June 1989. Also, USENIX Computing Systems, V2 no 4, Fall 1989, pp 367--396.Google ScholarGoogle Scholar
  48. Tom Duff. Duff's Device, Aug 1988. http://www.lysator.liu.se/c/duffs-device.html.Google ScholarGoogle Scholar
  49. D. Ungar and D. Patterson. Berkeley smalltalk: Who knows where the time goes? In G. Krasner, editor, Smalltalk-80: Bits of History andWords of Advice, pages 189--206. Addison-Wesley, 1983. ISBN 0-201-11669-3.Google ScholarGoogle Scholar
  50. J. Vitek, R. N. Horspool, and A. Krall. Efficient type inclu-sion tests. In Proc. of the 12th ACM SIGPLAN conference on Object-oriented programming, systems, languages, and appli-cations, OOPSLA '97, pages 142--157, New York, NY, USA, 1997. ACM. Google ScholarGoogle ScholarDigital LibraryDigital Library
  51. D. Vytiniotis, G. Washburn, and S. Weirich. An open and shut typecase. In Proc. of the 2005 ACM SIGPLAN international workshop on Types in languages design and implementation, TLDI '05, pages 13--24, New York, NY, USA, 2005. ACM. Google ScholarGoogle ScholarDigital LibraryDigital Library
  52. P.Wadler. The expression problem. Mail to the java-genericity mailing list, November 1998.Google ScholarGoogle Scholar
  53. D. Wasserrab, T. Nipkow, G. Snelting, and F. Tip. An operational semantics and type safety proof for multiple inheritance in C++. In Proc. of the 21st annual ACM SIGPLAN conference on Object-oriented programming systems, languages, and applications, OOPSLA '06, pages 345-362, New York, NY, USA, 2006. ACM. Google ScholarGoogle ScholarDigital LibraryDigital Library
  54. N. Wirth. Type extensions. ACM Trans. Program. Lang. Syst., 10(2):204--214, Apr. 1988. Google ScholarGoogle ScholarDigital LibraryDigital Library
  55. M. Zenger and M. Odersky. Extensible algebraic datatypes with defaults. In Proc. of the 6th ACM SIGPLAN international conference on Functional programming, ICFP '01, pages 241--252, New York, NY, USA, 2001. ACM. Google ScholarGoogle ScholarDigital LibraryDigital Library
  56. M. Zenger and M. Odersky. Independently extensible solutions to the expression problem. In Proc. FOOL 12, Jan. 2005.Google ScholarGoogle Scholar
  57. Y. Zibin and J. Y. Gil. Efficient subtyping tests with PQ-encoding. In Proc. of the 16th ACM SIGPLAN conference on Object-oriented programming, systems, languages, and applications, OOPSLA '01, pages 96--107, New York, NY, USA, 2001. ACM. Google ScholarGoogle ScholarDigital LibraryDigital Library

Index Terms

  1. Open and efficient type switch 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

      Full Access

      • Published in

        cover image ACM SIGPLAN Notices
        ACM SIGPLAN Notices  Volume 47, Issue 10
        OOPSLA '12
        October 2012
        1011 pages
        ISSN:0362-1340
        EISSN:1558-1160
        DOI:10.1145/2398857
        Issue’s Table of Contents
        • cover image ACM Conferences
          OOPSLA '12: Proceedings of the ACM international conference on Object oriented programming systems languages and applications
          October 2012
          1052 pages
          ISBN:9781450315616
          DOI:10.1145/2384616

        Copyright © 2012 ACM

        Publisher

        Association for Computing Machinery

        New York, NY, United States

        Publication History

        • Published: 19 October 2012

        Check for updates

        Qualifiers

        • research-article

      PDF Format

      View or Download as a PDF file.

      PDF

      eReader

      View online with eReader.

      eReader
      About Cookies On This Site

      We use cookies to ensure that we give you the best experience on our website.

      Learn more

      Got it!