type traits
play

Type Traits Generic programming and library development Michael - PowerPoint PPT Presentation

Introduction Type Traits In C++98 Type Traits In boost And TR1 Type Traits Generic programming and library development Michael Rasmussen og Klaus Birkelund Jensen Department of Computer Science - University of Copenhagen May 9, 2008 Michael


  1. Introduction Type Traits In C++98 Type Traits In boost And TR1 Type Traits Generic programming and library development Michael Rasmussen og Klaus Birkelund Jensen Department of Computer Science - University of Copenhagen May 9, 2008 Michael Rasmussen og Klaus Birkelund Jensen Department of Computer Science - University of Copenhagen Type Traits

  2. Introduction Type Traits In C++98 Type Traits In boost And TR1 Motivation Improve performance of generic algorithms based on advanced type information Specialize certain operations to better fit the type Deduce more information about the type Transform types into other types Michael Rasmussen og Klaus Birkelund Jensen Department of Computer Science - University of Copenhagen Type Traits

  3. Introduction Type Traits In C++98 Type Traits In boost And TR1 Examples template < typename I1 , typename I2 > I2 copy impl ( I1 f i r s t , I1 l a s t , I2 out ) w h i l e ( f i r s t != l a s t ) { ∗ out = ∗ f i r s t ; ++out ; ++f i r s t ; } } template < typename T > T ∗ copy impl (T ∗ f i r s t , T const ∗ l a s t ) { memcpy( out , f i r s t , ( l a s t − f i r s t ) ∗ s i z e o f (T ) ) ; r e t u r n out+( l a s t − f i r s t ) ; } Michael Rasmussen og Klaus Birkelund Jensen Department of Computer Science - University of Copenhagen Type Traits

  4. Introduction Type Traits In C++98 Type Traits In boost And TR1 Examples template < typename T > T f i n d r o o t ( T ( ∗ f )(T const &) ) { i f ( std : : n u m e r i c l i m i t s < T > :: i s i n t e g e r ) / ∗ f i n d exact root ∗ / e l s e / ∗ f i n d where c l o s e to zero ∗ / } Michael Rasmussen og Klaus Birkelund Jensen Department of Computer Science - University of Copenhagen Type Traits

  5. Introduction Type Traits In C++98 Type Traits In boost And TR1 Examples template < typename T > T f i n d r o o t ( T ( ∗ f )(T const &) ) { i f ( std : : n u m e r i c l i m i t s < T > :: i s i n t e g e r ) / ∗ f i n d exact root ∗ / e l s e / ∗ f i n d where c l o s e to zero ∗ / } template < typename I > std : : i t e r a t o r t r a i t s < T > :: v a l u e t y p e second ( I i t ) { ++i t ; r e t u r n ∗ i t ; } Michael Rasmussen og Klaus Birkelund Jensen Department of Computer Science - University of Copenhagen Type Traits

  6. Introduction Type Traits In C++98 Type Traits In boost And TR1 Implementation Use various tricks to deduce what the types are (e.g. pointers to member functions) Use template specialization (to determine if the type is const, volatile etc.) Explicitly specialize base types ( numeric types ) Michael Rasmussen og Klaus Birkelund Jensen Department of Computer Science - University of Copenhagen Type Traits

  7. Introduction Type Traits In C++98 Type Traits In boost And TR1 iterator traits template < c l a s s T > s t r u c t i t e r a t o r t r a i t s < T > { t y p e d e f / ∗ . . . ∗ / i t e r a t o r c a t e g o r y ; t y p e d e f / ∗ . . . ∗ / v a l u e t y p e ; t y p e d e f / ∗ . . . ∗ / d i f f e r e n c e t y p e ; t y p e d e f / ∗ . . . ∗ / p o i n t e r ; t y p e d e f / ∗ . . . ∗ / r e f e r e n c e ; } ; Michael Rasmussen og Klaus Birkelund Jensen Department of Computer Science - University of Copenhagen Type Traits

  8. Introduction Type Traits In C++98 Type Traits In boost And TR1 numeric limits template < c l a s s T > c l a s s n u m e r i c l i m i t s { p u b l i c : s t a t i c const bool i s s p e c i a l i z e d ; s t a t i c T min () throw ( ) ; s t a t i c T max () throw ( ) ; s t a t i c const i n t r a d i x ; s t a t i c const i n t d i g i t s ; s t a t i c const i n t d i g i t s 1 0 ; s t a t i c const bool i s s i g n e d ; s t a t i c const bool i s i n t e g e r ; s t a t i c const bool i s e x a c t ; // s n i p Michael Rasmussen og Klaus Birkelund Jensen Department of Computer Science - University of Copenhagen Type Traits

  9. Introduction Type Traits In C++98 Type Traits In boost And TR1 The Type Traits Library Included in the recently approved TR1 1 Uniform and portable interface Query, compare and modify type traits Based on boost’s type traits library 1 Technical Report One Michael Rasmussen og Klaus Birkelund Jensen Department of Computer Science - University of Copenhagen Type Traits

  10. Introduction Type Traits In C++98 Type Traits In boost And TR1 Design Type traits implemented as distinct classes. If the queried type has the given property the trait inherits from the class true type . Otherwise the trait inherits from false type . Michael Rasmussen og Klaus Birkelund Jensen Department of Computer Science - University of Copenhagen Type Traits

  11. Introduction Type Traits In C++98 Type Traits In boost And TR1 Simple Example template < typename T > s t r u c t i s v o i d : p u b l i c f a l s e t y p e {} ; template < > s t r u c t i s v o i d < void > : p u b l i c t r u e t y p e {} ; Michael Rasmussen og Klaus Birkelund Jensen Department of Computer Science - University of Copenhagen Type Traits

  12. Introduction Type Traits In C++98 Type Traits In boost And TR1 Example Usage template < typename I1 , typename I2 > i n l i n e I2 copy ( I1 f i r s t , I1 l a s t , I2 out ) { t y p e d e f typename std : : i t e r a t o r t r a i t s < I1 > :: v a l u e t y p e v a l u e t y p e ; r e t u r n copy imp ( f i r s t , l a s t , out , std : : h a s t r i v i a l a s s i g n < v a l u e t y p e > ()); } Michael Rasmussen og Klaus Birkelund Jensen Department of Computer Science - University of Copenhagen Type Traits

  13. Introduction Type Traits In C++98 Type Traits In boost And TR1 Example Usage template < typename I1 , typename I2 > i n l i n e I2 copy ( I1 f i r s t , I1 l a s t , I2 out ) { t y p e d e f typename std : : i t e r a t o r t r a i t s < I1 > :: v a l u e t y p e v a l u e t y p e ; r e t u r n copy imp ( f i r s t , l a s t , out , std : : h a s t r i v i a l a s s i g n < v a l u e t y p e > ()); } template < typename T > T ∗ copy imp ( const T ∗ f i r s t , const T ∗ l a s t , T ∗ out , const t r u e t y p e &); // optimized v e r s i o n template < typename I1 , typename I2 , bool b > I2 copy imp ( I1 f i r s t , I1 l a s t , I2 out , const i n t e g r a l c o n s t a n t < bool , b > &); // d e f a u l t − always s a f e Michael Rasmussen og Klaus Birkelund Jensen Department of Computer Science - University of Copenhagen Type Traits

  14. Introduction Type Traits In C++98 Type Traits In boost And TR1 Other Queries i s a r r a y i s c l a s s i s c o n s t i s f u n c t i o n i s i n t e g r a l i s m e m b e r o b j e c t p o i n t e r i s m e m b e r p o i n t e r i s p o d i s r e f e r e n c e Michael Rasmussen og Klaus Birkelund Jensen Department of Computer Science - University of Copenhagen Type Traits

  15. Introduction Type Traits In C++98 Type Traits In boost And TR1 Comparisons Compare base and derived classes: is base of c l a s s Base ; c l a s s Derived : p u b l i c Base ; is base of < Base, Derived > inherits from true type Michael Rasmussen og Klaus Birkelund Jensen Department of Computer Science - University of Copenhagen Type Traits

  16. Introduction Type Traits In C++98 Type Traits In boost And TR1 Transformations Add/remove const and volatile qualifiers. template < typename T > s t r u c t remove const { t y p e d e f T type ; } ; template < typename T > s t r u c t remove const < const T > { t y p e d e f T type ; } ; Note: remove const < const int ∗ > ::type evaluates to const int ∗ Michael Rasmussen og Klaus Birkelund Jensen Department of Computer Science - University of Copenhagen Type Traits

  17. Introduction Type Traits In C++98 Type Traits In boost And TR1 Sources Bjarne Stroustrup: The C++ Programming Language Special Edition Vandevoorde, Josuttis: C++ Templates - The Complete Guide boost http://www.boost.org TR1 articles: http://www.informit.com Michael Rasmussen og Klaus Birkelund Jensen Department of Computer Science - University of Copenhagen Type Traits

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend