library of assembled shared sources

http://lass.cocamware.com

lass::meta::TypeTraits< T > Class Template Reference

Interesting goodies on types. More...

#include <type_traits.h>


Data Structures

struct  ConstTraits
struct  ConstTraits< const U & >
struct  ConstTraits< const U >
struct  ConstTraits< U & >
struct  PointerTraits
struct  PointerTraits< U & >
struct  PointerTraits< U * >
struct  PointerTraits< U *& >
struct  ReferenceTraits
struct  ReferenceTraits< U & >

Public Types

enum  { isConst = ConstTraits<T>::isConst }
 is true if referred type of T is constant. More...
enum  { isPointer = PointerTraits<TNonConst>::isPointer }
 is true if referred type of T is a pointer More...
enum  { isReference = ReferenceTraits<T>::isReference }
 is true if T is a reference More...
typedef T Type
 type of T itself
typedef ConstTraits< T >::TNonConst TNonConst
 strips const qualifier of refered type of T (but keeps reference if any).
typedef ConstTraits< T >::TConst TConst
 add const qualifier to referred type of T (but keeps reference if any).
typedef PointerTraits
< TNonConst >::TPointee 
TPointee
 is type pointed to by referred type of T, NullType if not applyable.
typedef ReferenceTraits< T >
::TReferred 
TReferred
 is type T refers to if T is a reference, is T otherwise
typedef ReferenceTraits< T >
::TReference 
TReference
 is a reference to T if T is not a reference, is T otherwise
typedef ConstTraits< typename
ReferenceTraits< T >
::TReferred >::TNonConst 
TStorage
 strips const qualifier of refered type of T but without keeping reference if any.

Private Types

typedef ConstTraits< T >::TNonConst TStripped


Detailed Description

template<typename T>
class lass::meta::TypeTraits< T >

Interesting goodies on types.

Author:
Bram de Greve [BdG]

overview

TypeTraits gives you compile-time information on a type T. It will tell you if T is constant type, a pointer, a reference ... It will also able you to derive related types at compile time, such as non-const version of a constant type.

In contrary to the type traits in boost[1] or loki[2], our TypeTraits tries to be smarter concerning reference. i.e. if T is a reference, TypeTraits will also report properties of the referred type. And if you request a related type, it will modify the referred type instead. All this should make sure you get exactly what you intend. (this is still to be proven in practice [BdG]).

Here's a list of the different fields (we suppose T is the template parameter):

Type
a typedef to the template paramter. i.e. this is the same as T.
isConst
is true if T is a constant type or a reference to a constant type, is false otherwise. Notice that this will yield false for non-const pointers to const types (const int*) but true for const pointers to non-const types (int* const).
TNonConst
a typedef to the non-const version of T if T is const, otherwise it's T itself. You can use this to get rid of the const qualifier of a type, e.g. const int becomes int but const int* remains const int*. For references, the const qualifier is stripped of the referred type, e.g. const int& becomes int&.
TConst
This is the opposite of TNonConst. It appends a const qualifier to the (reffered) type, const int* becomes const int* const, int& becomes const int&
isPointer
is true if T is a pointer or a reference to a pointer, is false otherwise. Notice that int*& will yield true.
TPointee
if isPointer is true then TPointee is a typedef to the (referred) type points to, otherwise it will be equal to lass::meta::NullType. both int* const and int*& will yield int, but int and int& will yield lass::meta::NullType.
isReference
is true if T is a reference, is false otherwise. both const int& and int*& are references, const int and int* are not.
TReferred
if isReference is true then strips the ampersand to get type T is refering to, otherwise it's T itself. both const int& and const int become const int, int*& and int* become int*.
TReference
if isReference is false then adds the ampersand to get a type refering to a T, otherwise it's T itself. This solves the ill-formed reference-to-reference-to-type problem by keeping it a reference-to-type (what is exactly what you want). i.e. int becomes int&, but int& remains int&.
TStorage
yields in a type that can be used to store temporary objects. To store temporary objects, we must get rid of the reference and the const qualifier of the referred type. e.g. if a const std::string& is the type of a function parameter, you would like to store tempories as std::string. This TStorage will give you this std::string type.
Here we list all possible results of TypeTraits in a table. We show it for a base type int, but it is of course valid for any other type.

T isConst TNonConst TConst isPointer TPointee isReference TReferred TReference TStorage
int false int const int false NullType false int int& int
int* false int* int* const true int false int* int*& int*
int* const true int* int* const true int false int* const int* const& int*
int& false int& const int& false NullType true int int& int
int*& false int*& int* const& true int true int* int*& int*
int* const& true int*& int* const& true int true int* const int* const& int*
const int true int const int false NullType false const int const int& int
const int* false const int* const int* const true const int false const int* const int*& const int*
const int* const true const int* const int* const true const int false const int* const const int* const& const int*
const int& true int& const int& false NullType true const int const int& int
const int*& false const int*& const int* const& true const int true const int* const int*& const int*
const int* const& true const int*& const int* const& true const int true const int* const const int* const& const int*

Note:
You can see that references behave much the same as the refered type. e.g. TypeTraits<U>::TPointee will be the same as TypeTraits<typename TypeTraits<U>::TReferred>TPointee, no matter what the type of U is. The same is valid for isConst and isPointer. TConst and TNonConst are a bit different, for these the ampersand will again be added if U is a reference.

broken compilers workaround

Unfortunately, the have a neat implementation of TypeTraits, we need partial template specialisation, a feature not supported by MSVC6x and MSVC70. However, TypeTraits can be a crucial tool to implement algorithms that would be impossible to implement if TypeTraits fail. Thus we, do need a way to get this TypeTraits up and running in environments without this partial specialisation, and we need to get it to the right thing!

There's really only one way to solve this, and that's exhaustive full specialisation of all possible parameters T. This means we should specialise it for int, const int, const int*, ... (for each base type int, you have to specialise for the twelve cases as in the table above).

Of course, it is impossible for a library to do this, because we can never know all types that will ever be needed. Therefore we have a twofolded solution:

  namespace foo
  {
      struct Bar {};
  }
  LASS_META_BROKEN_TYPE_TRAITS_SPECIALISATION(foo::Bar)

Note:
you can safely use this macro regardless if the compiler supports partial specialisation. If your compiler supports it, the macro will be expanded to nothing.
Warning:
you have to call this macro outside any namespaces.

this exhaustive full specialisation might lead to an overflow of the compiler's internal heap while building. You're warned!

Definition at line 206 of file type_traits.h.


Member Typedef Documentation

template<typename T >
typedef ConstTraits<T>::TNonConst lass::meta::TypeTraits< T >::TStripped [private]

Definition at line 269 of file type_traits.h.

template<typename T >
typedef T lass::meta::TypeTraits< T >::Type

type of T itself

Definition at line 275 of file type_traits.h.

template<typename T >
typedef ConstTraits<T>::TNonConst lass::meta::TypeTraits< T >::TNonConst

strips const qualifier of refered type of T (but keeps reference if any).

Definition at line 280 of file type_traits.h.

template<typename T >
typedef ConstTraits<T>::TConst lass::meta::TypeTraits< T >::TConst

add const qualifier to referred type of T (but keeps reference if any).

Definition at line 282 of file type_traits.h.

template<typename T >
typedef PointerTraits<TNonConst>::TPointee lass::meta::TypeTraits< T >::TPointee

is type pointed to by referred type of T, NullType if not applyable.

Definition at line 287 of file type_traits.h.

template<typename T >
typedef ReferenceTraits<T>::TReferred lass::meta::TypeTraits< T >::TReferred

is type T refers to if T is a reference, is T otherwise

Definition at line 292 of file type_traits.h.

template<typename T >
typedef ReferenceTraits<T>::TReference lass::meta::TypeTraits< T >::TReference

is a reference to T if T is not a reference, is T otherwise

Definition at line 294 of file type_traits.h.

template<typename T >
typedef ConstTraits<typename ReferenceTraits<T>::TReferred>::TNonConst lass::meta::TypeTraits< T >::TStorage

strips const qualifier of refered type of T but without keeping reference if any.

Definition at line 297 of file type_traits.h.


Member Enumeration Documentation

template<typename T >
anonymous enum

is true if referred type of T is constant.

Enumerator:
isConst 

Definition at line 278 of file type_traits.h.

template<typename T >
anonymous enum

is true if referred type of T is a pointer

Enumerator:
isPointer 

Definition at line 285 of file type_traits.h.

template<typename T >
anonymous enum

is true if T is a reference

Enumerator:
isReference 

Definition at line 290 of file type_traits.h.


The documentation for this class was generated from the following file:

Generated on Mon Nov 10 14:22:10 2008 for Library of Assembled Shared Sources by doxygen 1.5.7.1
SourceForge.net Logo