{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

C++_type_programming - A General Look at Type Programming...

Info icon This preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE 332: C++ Type Programming: Associated Types, Typedefs and Traits A General Look at Type Programming in C++ Associated types (the idea) Let you associate a type with another type Given a type parameter, let you get the other type Let you control encapsulation of type information Typedefs + specialization (the mechanisms) Let you give a known type another name Let you name associated types consistently Across user-defined/built-in const/non-const types Traits (the technique) Let you associate user-defined and built-in types Let you provide consistent access across types
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
CSE 332: C++ Type Programming: Associated Types, Typedefs and Traits Basic Example: Associated Types (the Idea) Want to swap the values of two locations in memory Basic idea is simple Declare a temporary variable “Remember” value from location i1 aliases in temp Copy value from location i2 aliases into location i1 aliases Copy “remembered” value from temp into location i2 aliases But, code to left won’t compile How can we declare temp ’s type? How can we get T from I ? // Based on Austern pp. 34 template <typename I> void swap (I i1, I i2) { T temp = *i1; *i1 = *i2; *i2 = temp; }
Image of page 2
CSE 332: C++ Type Programming: Associated Types, Typedefs and Traits Typedef + Specialization (the Mechanisms) Use specialization of a general traits struct template Still have to parameterize struct iterator_traits with typename I Can’t do full specialization since we’re using type parameter T Gives us different versions of the iterator_traits struct For user-defined, built-in types For const, non-const types Remember that the C++ compiler will select the most specific match // Based on Austern, pp. 35 template <typename I> struct iterator_traits { typedef typename I::value_type value_type; }; template <typename T> struct iterator_traits<T*> { typedef T value_type; }; template <typename T> struct iterator_traits<const T*> { typedef T value_type; };
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
CSE 332: C++ Type Programming: Associated Types, Typedefs and Traits Traits (the Technique) Given the iterator type I Traits provide an interface to I ‘s associated types E.g., iterator_traits<I>::value_type Let us obtain and use those types in template code In general, let us go from containers to iterators to traits to associated types Even within templates template <typename I> void swap (I i1, I i2) { iterator_traits<I>::value_type temp = *i1; *i1 = *i2; *i2 = temp; }
Image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern