假设我有以下函数,该函数将一个函数作为参数。
就像
使用function_traits(例如Boost中的那些),我可以获取
如何生成所需数量的函数调用,使其与函数F的元数匹配?
在C++11之前,我会看看Boost.Preprocessor,或者可能依赖于模板特化。现在有更好的方法吗?
template <typename F>
void test_func(F f)
{
// typedef typename function_traits<F>::return_type T;
typedef int T;
std::mt19937 rng(std::time(0));
std::uniform_int_distribution<T> uint_dist10(0, std::numeric_limits<T>::max());
f(uint_dist10(rng), uint_dist10(rng)); // Problem!
}
使用方法如下:
int foo(int, int) { return 0; }
int bar(int, int, int, int) { return 0; }
int main()
{
test_func(foo);
// test_func(bar);
}
就像
foo
和bar
一样,我有几个返回T
的函数,并且需要一些类型为T
的参数。我希望test_func
调用尽可能多的随机数生成器来匹配函数f所需的参数数量。换句话说,我们可以假设T
始终是整数类型,并且每个参数都相同,即对随机数生成器进行函数调用。使用function_traits(例如Boost中的那些),我可以获取
F
的返回类型,这有点帮助。大致上,我的问题是:如何生成所需数量的函数调用,使其与函数F的元数匹配?
在C++11之前,我会看看Boost.Preprocessor,或者可能依赖于模板特化。现在有更好的方法吗?