SE SUPONE QUE TYPEAT TEMPLATE DEBE DEVOLVER EL TYPO DE ENTRE
LOS DIFENTES TYPOS INCLUIDOS EN UN CONTAINER LLAMADO
CONSSe supone que typeat template
debe devolver el type
de entre
ESLos difentes types
incluidos en un container
llamado Cons
Es del libro advanced metaprogramming g++ v4.9 tdm windowsadvanced metaprogramming g++ v4.9 tdm windows
#include <iostream>
#include <type_traits>
using namespace std;
template < typename T1, typename T2 >
struct typepair { typedef T1 head_t; typedef T2 tail_t; };
struct empty { };
struct Error_UNDEFINED_TYPE; // no definition!
template < size_t N, typename CONTAINER, typename ERR = Error_UNDEFINED_TYPE
>
struct typeat
{
typedef ERR type;
};
template<typename T1,typename T2,typename ERR>
struct typeat < 0, typepair < T1, T2 >, ERR >
{
typedef T1 type;
};
template < size_t N, typename T1, typename T2, typename ERR >
struct typeat < N, typepair < T1, T2 >, ERR >
{
typedef typename typeat < N-1, T2, ERR >::type type;
};
template < size_t N,typename ERR >
struct typeat < N, empty, ERR >
{
typedef empty type;
};
template < typename T1, typename T2, typename ...Args >
struct cons
{
typedef typepair < T1, typepair < T2, Args... > > type;
};
int main(int argc,char **argv){
typedef cons<float,float,int,empty> mio_t;
typedef typename typeat<1,cons<float,float,int,empty>>::type final;
std::cout << std::boolalpha << std::is_floating_point<final>::value <<
std::endl;
//CONTROL ERRORES
std::cout << std::boolalpha <<
std::is_same<final,Error_UNDEFINED_TYPE>::value << std::endl;
std::cout << std::boolalpha << std::is_same<final,empty>::value <<
std::endl;
return 0;
};