I have an application where I'm building a function, marshal_and_apply
, which calls some other function (or functor), f
with some arguments. marshal_and_apply
's job is to apply some special marshaling for the arguments depending on the type of f
's parameters.
If one of f
's parameters is of a special type, marshal_me<T>
, then marshal_and_apply
will marshal the parameter through some specially allocated storage before passing it to f
. In order to perform the allocation, the storage requirements of all the parameters must be known to marshal_and_apply
before any can be marshaled.
Some examples:
template<typename Function, typename... Args>
void marshal_and_apply(Function f, Args... args);
void func1(int x, int y);
void func2(marshal_me<int> x, int y);
void func3(marshal_me<int> x, marshal_me<int> y, marshal_me<int> z);
// this call would be equivalent to:
// func1(7,13)
marshal_and_apply(func1, 7, 13);
// this call would be equivalent to:
// auto storage = my_allocator(sizeof(int));
// auto x = marshal_me<int>(7, storage);
// func2(x, 13);
marshal_and_apply(func2, 7, 13);
// this call would be equivalent to:
// auto storage = my_allocator(sizeof(int) + sizeof(int) + sizeof(int));
// auto x = marshal_me<int>(7, storage);
// auto y = marshal_me<int>(13, storage + sizeof(int));
// auto z = marshal_me<int>(42, storage + sizeof(int) + sizeof(int));
// func3(x,y,z);
marshal_and_apply(func3, 7, 13, 42);
To solve this problem, it seems that marshal_and_apply
requires a mechanism to inspect the types of f
's parameters. I suspect this isn't possible in general, but it may be possible to recognize whether one of a special set of types (in this case, marshal_me<T>
) is convertible to the type of a particular parameter.
How should I build marshal_and_apply
?