Sign in to follow this  

typelist to function parameters

This topic is 3590 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

ive started reading "Moden C++ Design" and ive just finished the typelist chapter im wondering if it would be possible to use a typelist as function parameters using the preprocessor or something similar... template <typename TList> class A { void DoSomething(EXPAND_TYPES(TList)) {} } and it would look something like... A<TYPELIST1(int)> first; first.DoSomething(0); A<TYPELIST2(int, int)> second; second.DoSomething(0,0); EDIT:: EXPAND_TYPES would expand the types so that all params have their own predefined name something like param1, param2 etc... so (EXPAND_TYPES(TYPELIST1(int,int))) => (int param1, int param2)

Share this post


Link to post
Share on other sites
Don't use the preprocessor. Just template on one parameter, which accumulates all the data. I haven't read the book so I don't know exactly what a "typelist" is in this context, but I imagine something like


template <typename Car, typename Cdr>
struct Typelist {
typedef Car car_t;
typedef Cdr cdr_t;
Car car;
Car cdr;
Typelist(const Car& car, const Cdr& cdr): car(car), cdr(cdr) {}
};

template <typename Car, typename Cdr>
Typelist<Car, Cdr> cons(const Car& car, const Car& cdr) {
return Typelist(car, cdr);
}


In that case, all you need to do is something like


template <typename L>
void doSomething(const L& list) {
// will fail to compile for lists of fewer than three elements
typename L::car_t& first = list.car;
typename L::cdr_t::car_t& second = list.cdr.car;
typename L::cdr_t::cdr_t::car_t& third = list.cdr.cdr.car;
// manipulate them
}

Share this post


Link to post
Share on other sites
Zahlman typelists are compile time(traversed in, nodes removed etc...)
Dragon_Strikea your question is answered in a later section named functors(or similar). Here is some information about problems with loki's typelists and macros, see the link to the article on dr.bobb’s.

Share this post


Link to post
Share on other sites
hmm... i havent rly found what im looking for... in the "functor chaper" he manually creates all the versions of operator()... however id like to automaticly created them from a TypeList

the closest thing ive found is BOOST_PP_ENUM_PARAMS

BOOST_PP_ENUM_PARAMS(3, class T) // expands to class T0, class T1, class T2

however id like something like

ENUM_TYPELIST_PARAMS(TYPELIST3(A, B, C) // expands to class A0, class B0, class C0

where A,B,C are classes or typenames..

Share this post


Link to post
Share on other sites
Quote:
Original post by Dragon_Strike
hmm... i havent rly found what im looking for... in the "functor chaper" he manually creates all the versions of operator()... however id like to automaticly created them from a TypeList


You can't.

You can use typelists to process the data in class, specify members, or otherwise declare members of the class, but you cannot use it to specify function parameters. For that, you still need pre-processor and hard-code the values.

This should be possible in next revision of C++ standard.

To quote Morbo: "Function parameters do not work that way!"

Share this post


Link to post
Share on other sites

This topic is 3590 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this