framework for invoking func-ptrs with different argument numbers
Hi,
Consider the following task: Given a
-) pointer to a function, and
-) an iterator range with elements of type void *,
the function-ptr shall be called with all the elements in the range as
arguments.
So conceptionally something like this:
template
2013/8/17 firespot
Hi,
Consider the following task: Given a -) pointer to a function, and -) an iterator range with elements of type void *, the function-ptr shall be called with all the elements in the range as arguments.
So conceptionally something like this:
template
void make_call(Func, Iter begin, Iter end) { int n = std::distance(begin, end); (*Func) (*(begin), *(begin + 1), *(begin + 2), ..., *(begin + n - 1)) } The interface (function declaration) here is +- what I want, the implementation shown of course is dummy-code not working. Does boost provide a more or less easy way to get this implementation smoothly done? C++ is 2003 version, so code must be compatible with e.g. VS 2008.. Note that I would know in principle a way how to do implement it myself, which involves the creation of a separate class for each number of arguments, but that's precisely what I want to avoid and I hope that wheel has already been invented in some boost-library :)
It'll be much easier if the iterator is Fusion iterator, do you really need dynamic-ranged args for a member function? What if the size mismatched? to fill with null args?
Consider the following task: Given a -) pointer to a function, and -) an iterator range with elements of type void *, the function-ptr shall be called with all the elements in the range as arguments.
So conceptionally something like this:
template
void make_call(Func, Iter begin, Iter end) { int n = std::distance(begin, end); (*Func) (*(begin), *(begin + 1), *(begin + 2), ..., *(begin + n - 1)) } The interface (function declaration) here is +- what I want, the implementation shown of course is dummy-code not working. Does boost provide a more or less easy way to get this implementation smoothly done? C++ is 2003 version, so code must be compatible with e.g. VS 2008.. Note that I would know in principle a way how to do implement it myself, which involves the creation of a separate class for each number of arguments, but that's precisely what I want to avoid and I hope that wheel has already been invented in some boost-library :)
Is 'Func' a C-style vararg function, or is it a function object where the number of arguments it takes is known at compile time (perhaps being a choice among a finite number of overloads)? Regards, Nate
On Aug 16, 2013, at 1:14 PM, firespot
Does boost provide a more or less easy way to get this implementation smoothly done? C++ is 2003 version, so code must be compatible with e.g. VS 2008..
This example is the classic approach: http://www.boost.org/doc/libs/1_54_0/libs/function_types/example/interpreter... In my case I had what amounts to an array of boost::variant with certain scalar types, but it was fairly straightforward to convert from "parsing the next argument" to "using the next array entry." And yes, it's C++03 compatible. Give it a try, and good luck!
participants (4)
-
firespot
-
Nat Goodspeed
-
Nathan Ridge
-
TONGARI J