#define DECLARE_VECTOR(vector_name) \ template< int i, int bidon > \ struct vector_name \ { \ enum \ { \ is_defined = 0 \ }; \ } #define SET_VECTOR_ELEMENT(vector_name, i, element) \ template< int bidon > \ struct vector_name \ { \ enum \ { \ is_defined = 1 \ }; \ typedef element value; \ } template< bool b, typename i, typename j > struct select_value; template< typename i, typename j > struct select_value< true, i, j > { enum { value = i::value }; }; template< typename i, typename j > struct select_value< false, i, j > { enum { value = j::value }; }; template< int i > struct IntToType { enum { value = i }; }; template< int i, template< int, int > class TypeVector, int IdUnique > struct LnVector { enum { value = select_value< TypeVector::is_defined, LnVector< i + 1, TypeVector, IdUnique >, IntToType >::value }; }; #define GET_VECTOR_LENGTH( vector_name ) \ LnVector< 0, vector_name, __LINE__ >::value #define PUSH_BACK_IN_VECTOR( vector_name, element ) \ template< int bidon > \ struct vector_name< GET_VECTOR_LENGTH(vector_name), bidon > \ { \ enum \ { \ is_defined = 1 \ }; \ typedef element value; \ } DECLARE_VECTOR( MyVector ); SET_VECTOR_ELEMENT( MyVector, 0, int ); SET_VECTOR_ELEMENT( MyVector, 1, float ); PUSH_BACK_IN_VECTOR( MyVector, double ); PUSH_BACK_IN_VECTOR( MyVector, long ); template< typename T, typename U > struct TypesAreEqual { enum { value = false }; }; template< typename T > struct TypesAreEqual< T, T > { enum { value = true }; }; #define GET_VECTOR_ELEMENT( vector_name, i ) \ vector_name< i, __LINE__>::value int main(int argc, char *argv[]) { cout << "lenght : " << GET_VECTOR_LENGTH(MyVector) << endl; // prints 4 cout << TypesAreEqual< int, GET_VECTOR_ELEMENT(MyVector, 0) >::value << endl; // prints 1 cout << TypesAreEqual< float, GET_VECTOR_ELEMENT(MyVector, 1) >::value << endl; // prints 1 cout << TypesAreEqual< double, GET_VECTOR_ELEMENT(MyVector, 2) >::value << endl; //prints 1 cout << TypesAreEqual< long, GET_VECTOR_ELEMENT( MyVector, GET_VECTOR_LENGTH(MyVector)-1) >::value << endl; // prints 1 return EXIT_SUCCESS; }