我有一个将被用作不同的论点职能一些自定义类型:递归调用重载C++函数
struct A {};
struct B {};
struct C {};
struct D {};
struct E {};
也有一些函数返回仿函数包装:
template <typename H>
auto foo (H h,
enable_if_t<is_same<typename result_of<H(A)>::type, bool>::value>* = 0)
{
return [h] (B x) { return h (A {}); };
}
这把H(A)仿函数转换成G(B)仿函数,它将输入参数B-> A(在这里为简单起见不实现)并且用A来调用H。
我有类似的con vertors C-> B,D-> C,E-> d:
template <typename H>
auto foo (H h,
enable_if_t<is_same<typename result_of<H(B)>::type, bool>::value>* = 0)
{
return [h] (C x) { return h (B {}); };
}
template <typename H>
auto foo (H h,
enable_if_t<is_same<typename result_of<H(C)>::type, bool>::value>* = 0)
{
return [h] (D x) { return h (C {}); };
}
template <typename H>
auto foo (H h,
enable_if_t<is_same<typename result_of<H(D)>::type, bool>::value>* = 0)
{
return [h] (E x) { return h (D {}); };
}
现在我可以调用foo 4次,将得到获取型 “E” 的说法函子,最后调用与内部处理程序参数“A”:
auto inner_handler = [] (A) -> bool { return false; };
auto f = foo (foo (foo (foo (inner_handler))));
f (E {});
我想是实现call_until功能称之为“foo”的重载递归直到产生仿函数参数类型成为T.
假设转换器的从A路径到E永远存在,并且恰好是一个。换句话说,我想表达
auto f = call_until<E> (inner_handler);
工作完全一样
auto f = foo (foo (foo (foo (inner_handler))));
我是从像开始:
template <typename Stop, typename Handler, typename Result>
struct call_until_helper
{
Handler handler_;
call_until_helper (Handler h) : handler (h) {}
};
template <typename Stop, typename Handler>
call_until_helper<Stop, Handler,
typename boost::function_traits<Handler>::result_type>
call_until (Handler handler)
{
return call_until_helper<Stop, Handler,
typename boost::function_traits<Handler>::result_type> (handler);
}
但我得到的编译错误还挺停留在这点。我需要一些想法来实现这一点。
尝试使用' - >'表示“从左手边接受参数,并返回右边”,因为这更传统的功能符号相匹配。所以有一个超载'foo:''H:A-> bool'' - >''G:B-> bool'。为了让你的代码更清洁,你可以用'result_of_t'替换'result_of'吗?如果您的编译器没有它,请使用result_of_t = typename std :: result_of :: type;'编写一个模板。噪音较小。 –
Yakk
2015-02-06 20:07:41