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

Info iconThis preview shows pages 1–6. 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
Background image of page 1

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

View Full DocumentRight 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; }
Background 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; };
Background image of page 3

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

View Full DocumentRight 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; }
Background image of page 4
CSE 332: C++ Type Programming: Associated Types, Typedefs and Traits
Background image of page 5

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

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/03/2010 for the course CSE 332S taught by Professor Smart during the Spring '08 term at Washington University in St. Louis.

Page1 / 14

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

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online