Boost logo

Boost :

From: David A. Greene (greened_at_[hidden])
Date: 2002-12-06 11:59:44


David Abrahams wrote:

>>I suppose not. What I really wanted was the ability to take a
>>regular old template class and create a generator out of it:
>>
>>template<typename T, typename U, typename V>
>>struct my_type { ... } // Note: no ::type member
>>
>>typedef SHAZAM<my_type<int, _1, _2> > generator;
>>
>>typedef generator::template apply<char, float>::type my_type_inst;
>>
>>I want SHAZAM to be generic enough to take any template class,
>>not just my_type.
>
> You can do that, up to N arguments, for some N.

But consider this testcase:

//#define CT_DEBUG

//#include "ct_print.hh"
#include <boost/mpl/placeholder.hpp>
#include <boost/mpl/lambda.hpp>

template<typename T, typename U, typename V>
struct my_type {
};

template<typename T, typename U, typename V>
struct my_type_generator {
   typedef my_type<T, U, V> type;
};

template<template<typename, typename, typename> class Base,
         typename T, typename U, typename V>
struct trivial_generator {
   typedef Base<T, U, V> type;
};

int main(void)
{
   typedef my_type_generator<char, short, int> generator;
   //CT_PRINT(my_type_generator_result_is, generator::type);

   typedef trivial_generator<my_type, char, short, int> t_generator;
   //CT_PRINT(trivial_generator_result_is, t_generator::type);

   // Try to make some generators
   typedef boost::mpl::lambda<my_type_generator<char, boost::mpl::_1,
     boost::mpl::_2> >::type lambda_generator;
   typedef lambda_generator::apply<short, int> lg_apply_result;
   //CT_PRINT(lambda_generator_result_is, lg_apply_result::type);

   // Does not work
   typedef boost::mpl::lambda<trivial_generator<my_type, char,
     boost::mpl::_1, boost::mpl::_2> >::type lambda_trivial_generator;
   // g++ 3.2 claims apply doesn't exist.
   typedef lambda_trivial_generator::apply<short, int> ltg_result;
   //CT_PRINT(lambda_trivial_generator_result_is, ltg_result::type);

   return(0);
}

Here's what g++ 3.2 tells me:

lambda_test.cc: In function `int main()':
lambda_test.cc:38: ISO C++ forbids declaration of `apply' with no type
lambda_test.cc:38: template-id `apply<short int, int>' used as a
   declarator
lambda_test.cc:38: typedef name may not be class-qualified
lambda_test.cc:38: parse error before `;' token

>>But I can construct a generic trivial generator:
>>
>>template<template<typename A, typename B, typename C> class Base,
>> typename T, typename U, typename V>
>>struct trivial_generator {
>> typedef Base<T, U, V> type;
>>};
>>
>>Then I can use MPL lambda facilities. Unfortunately, I need a
>>trivial_generator for every arity of class template.
>
> Only as part of the implementation. You just need a nice wrapper over
> the top to hide it all.

Right. Can you point me to the MPL equivalent as a guide of
how to do this?

                       -Dave

-- 
"Some little people have music in them, but Fats, he was all music,
  and you know how big he was."  --  James P. Johnson

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk