PROGRAMMING TECHNIQUES Based on “Modern C++ Design” BY ALEXANDRESCU BOGDAN PENKOVSKY 2010

Embed Size (px)

DESCRIPTION

PROGRAMMING TECHNIQUES WHAT’S ALL THIS STAFF ABOUT? Through all the “Modern C++ design” some essential techniques are stretched. Bold knowledge of them will help to understand main ideas and… create powerful idioms. So, we’re going to review and discuss main PROGRAMMING TECHNIQUES

Citation preview

PROGRAMMING TECHNIQUES Based on Modern C++ Design BY ALEXANDRESCU BOGDAN PENKOVSKY 2010 PROGRAMMING TECHNIQUES WHATS ALL THIS STAFF ABOUT? PROGRAMMING TECHNIQUES WHATS ALL THIS STAFF ABOUT? Through all the Modern C++ design some essential techniques are stretched. Bold knowledge of them will help to understand main ideas and create powerful idioms. So, were going to review and discuss main PROGRAMMING TECHNIQUES PROGRAMMING TECHNIQUES TECHNIQUES Compile-Time Assertions Partial Template Specialization Local Classes Mapping Integral Constants to Types Type-to-Type Mapping PROGRAMMING TECHNIQUES TECHNIQUES Type Selection Detecting Convertibility and Inheritance at Compile Time A Wrapper Around type_info NullType and EmptyType Type Traits PROGRAMMING TECHNIQUES The need for better static checking (and more customizable error messages) template To safe_reinterpret_cast(From from) { assert (sizeof(From) ::exists ' '; } PROGRAMMING TECHNIQUES Conversion We can implement one more constant inside Conversion: sameType, which is true if T and U represent the same type: template class Conversion {... as above... enum { sameType = false }; }; PROGRAMMING TECHNIQUES Conversion We implement sameType through a partial specialization of Conversion template class Conversion { public: enum { exists = 1, sameType = 1 }; }; PROGRAMMING TECHNIQUES SuperSubClass Macro //With the help of Conversion, it is now very //easy to determine inheritance #define SUPERSUBCLASS(T, U) \ (Conversion ::exists\ && \ !Conversion ::sameType); PROGRAMMING TECHNIQUES SuperSubClass Macro SUPERSUBCLASS(T, U) evaluates to true if U inherits from T publicly, or if T and U are actually the same type. SUPERSUBCLASS does its job by evaluating the convertibility from a const U* to a const T*. PROGRAMMING TECHNIQUES In the next slides: A wrapper around type_info NullType and EmptyType the helpful helpers ;) and finally: TypeTraits PROGRAMMING TECHNIQUES Standard type_info class std::type_info gives the ability to investigate object types at runtime Hard to exploit: Disables the copy constructor and assignment operator, which makes storing type_info objects impossible. Does not guarantee that each invocation returns a reference to the same type_info object. PROGRAMMING TECHNIQUES Loki wrapper class solution class TypeInfo { public: TypeInfo(); TypeInfo(const std::type_info&); TypeInfo(const TypeInfo&); TypeInfo& operator==(const TypeInfo&); bool before(const TypeInfo&) const; const char* name() const; private: const std::type_info* pInfo_; } PROGRAMMING TECHNIQUES Loki wrapper class solution // Comparison operators bool operator==(const TypeInfo&, const TypeInfo&); bool operator!=(const TypeInfo&, const TypeInfo&); bool operator=(const TypeInfo&, const TypeInfo&); PROGRAMMING TECHNIQUES Loki wrapper class solution // Now you can directly compare objects of type TypeInfo and std::type_info void Fun(Base* pObj) { TypeInfo info = typeid(Derived);... if (typeid(*pObj) == info) {... pBase actually points to a Derived object... }... } PROGRAMMING TECHNIQUES Important features The ability to copy and compare TypeInfo objects is important in many situations. The cloning factory in and one double- dispatch engine provided in future seminars put TypeInfo to good use. PROGRAMMING TECHNIQUES NullType & EmptyType helper Classes class NullType {}; Used for cases in which a type must be there syntactically but doesn't have a semantic sense. struct EmptyType {}; EmptyType is a legal type to inherit from, and you can pass around values of type EmptyType Will be used later in TypeLists PROGRAMMING TECHNIQUES Type Traits Traits are a generic programming technique that allows compile-time decisions to be made based on types, much as you would make runtime decisions based on values Extra level of indirection" that solves many software engineering problems Type-related decisions outside the immediate context in which they are made. PROGRAMMING TECHNIQUES Copying application template OutIt Copy(Init first, Init last, OutIt result) { for (; first != last; ++first, ++result) *result = *first; } PROGRAMMING TECHNIQUES Implementing Pointer Traits //T is not a pointer, and a pointee type doesn't apply. template class TypeTraits { private: template struct PointerTraits { enum(result = false }; typedef NullType PointeeType; }; PROGRAMMING TECHNIQUES Implementing Pointer Traits //A better match than the generic template for any pointer type. template struct PointerTraits { enum { result = true }; typedef U PointeeType; }; public: enum { isPointer = PointerTraits ::result }; typedef PointerTraits ::PointeeType PointeeType; }; PROGRAMMING TECHNIQUES Summary