Pregunta

I have a Typelist implementation very similar to this one. If you don't know what a typelist is: in short, it behaves like variadic templates using nested tuples. You can read more about them here.

I would like to build up a function pointer type from a subset of this typelist. The subset is defined by a list of indexes (of any size) and the desired operation does a lookup of these indexes in the typelist and defines the type of a pointer on a function using those types as arguments.

The API would look something like:

#include "typelist.h"
// typelist definition
typedef Typelist<float, Typelist<double, Typelist<int, NullType>>> Pixel;

typedef FunctionFromFields<Pixel, 0, 2>::Type field_0_and_2;
// I want the definition above to be equivalent to:
// typedef void (*field_0_and_2)(float*, int*);

It seems reasonable to assume this is possible since everything is known at compile time, but I have yet to find the right syntax for that.

I don't want to use variadic templates to replace the typelist, however they can be used to define the pointer type.

Has anyone done something similar?

¿Fue útil?

Solución

This should be fairly easy. First define a typelist_nth type function (left as an exercise; I assume you have one in your typelist.h):

template<typename TL, int I> struct typelist_nth;

Then use a variadic template to build a function type:

template<typename TL, int... Is> struct FunctionFromFields {
    typedef void (*Type)(typename typelist_nth<TL, Is>::type *...);
};
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top