why does not this program work?

1

It is assumed that typeat template should return the type of between Differs types included in a container called Cons

It's from the book advanced 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;
};
    
asked by FJsanto 04.04.2017 в 23:43
source

1 answer

1

The problem you have when using cons is that you end up invoking this implementation:

template < size_t N, typename CONTAINER, typename ERR = Error_UNDEFINED_TYPE >
struct typeat
{ 
  typedef ERR type;
};

And that's why you always receive the error you mention. You have a concept error The templates that you have at the beginning expect to receive a sequence such that:

typepair<float,typepair<float,typepair<int,empty>>>

Instead of what you are currently facilitating:

cons<float,float,int,empty>

With what the line in question would look like this:

typedef typename typeat<1,typepair<float,typepair<float,typepair<int,empty>>>>::type final;

With this, the program should compile. If you wanted to use cons you would have to make some changes. The first thing you need is a structure that allows you to convert Args... in typepair<T1,typepair<T2,...>> :

template<typename ...Args>
struct pack;

template< typename T1, typename T2 >
struct pack<T1,T2>
{
  typedef typepair<T1,T2> type;
};

template < typename T1, typename ...Args >
struct pack<T1,Args...>
{
  typedef typename pack<Args...>::type nestedPair;
  typedef typepair<T1,nestedPair> type;
};

And now we just need to modify const :

template <typename ...Args >
struct cons 
{ 
  typedef typename pack<Args...>::type type; 
};

And now the test of fire:

typedef cons<float,float,int,empty> mio_t; 
typedef typename  typeat<1,mio_t::type>::type final;
std::cout << std::boolalpha << std::is_floating_point<final>::value << std::endl;
    
answered by 06.04.2017 / 14:00
source