Lecture overview -- Keyboard shortcut: 'u'  Previous page: Templates -- Keyboard shortcut: 'p'  Next page: Example - Point<C> -- Keyboard shortcut: 'n'  Lecture notes - all slides and notes together  slide -- Keyboard shortcut: 't'  Help page about these notes  Alphabetic index  Course home  Lecture 5 - Page 3 : 39
Notes about C++
Templates and The Standard Library
Templates versus generics in Java, C# and C++

Templates in C++ can be compared with generics in Java and C#

  • Java

    • Type erasure: type parameters are compiled away.

      • Problems with reflection - cannot reestablish actual type parameters

    • Wild cards - an actual type parameter mechanism

      • Alleviates the covariance and contra variance problems with generic instance

  • C#

    • Reflection with generic types is possible

    • Support of constraints on formal parameter types: where clauses

      • where class;   where struct;   where new();   where C;   where C, I

    • When value types are used as actual type parameters

      • Separate instantiations are generated

    • C#4.0: Interfaces can be defined as covariant (out) or contravariant (in)

  • C++

    • Template specialization, and partial specialization

      • Allow alternative implementation for certain template parameters

      • To avoid code bloat in some situations

        • Containers of pointers share their implementation     (§13.5)

    • Powerful compile time calcuations - Turing complete

    • Templates cannot be compiled as such     (§13.2.5)

      • The instantiations are compiled

      • Only relatively superficial compiler check of the template - before any instantiation

    • Relies on compile-time duck typing