- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一些自定义类型将用作函数的不同参数:
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。
我有类似的转换器 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);
}
但是我遇到了编译错误并且有点卡在了这一点上。我需要一些想法来实现它。
最佳答案
您最接近的问题是 function_traits
需要函数类型,而不是 lambda 类型。要确定这一点,只需找到错误,提取导致它的语句,剥离类型并直接将它们传递进去。
boost::function_traits< decltype( inner_handler ) >::result_type b = false;
编译失败。然后我检查了文档,是的,它需要一个函数类型,而不是 lambda。 Lambda 不是函数。
这是您实际问题附近的问题解决方案草图。语法有点不同,因为我很懒。
这里是fooify
。它代表单个对象中 foo
的整个重载集:
struct fooify {
template<class...Args>
auto operator()(Args&&...args)const{
return foo(std::forward<Args>(args)...);
}
};
这是一种辅助类型,它递归地将操作应用于输入,直到通过测试为止:
template<class Action, template<class...>class test, class Arg, class=void>
struct repeat_until_helper {
using action_result = result_of_t< Action&(Arg) >;
auto operator()(Action&& action, Arg&&arg)const {
return repeat_until_helper<Action, test, action_result>{}(
std::forward<Action>(action),
action( std::forward<Arg>(arg) )
);
}
};
template<class Action, template<class...>class test, class Arg>
struct repeat_until_helper< Action, test, Arg,
std::enable_if_t< test<Arg>{} >
> {
auto operator()(Action&& action, Arg&&arg)const {
return std::forward<Arg>(arg);
}
};
这是一个使用了上面的helper的函数,所以我们只需要传入一个类型(测试),其余推导:
template<template<class...>class test, class Action, class Arg>
auto repeat_until( Action&& action, Arg&& arg) {
return repeat_until_helper< Action, test, Arg >{}( std::forward<Action>(action), std::forward<Arg>(arg) );
}
下面是对使用 E
右值调用的类型的测试:
template<class X, class=void>
struct can_be_called_with_E:std::false_type{};
template<class X>
struct can_be_called_with_E<X,
decltype(
void(
std::declval<X>()(std::declval<E>())
)
)
>:std::true_type{};
我们完成了。语法不同,但这只是一些需要整理的工作。
关于c++ - 递归调用重载的c++函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28373514/
C语言sscanf()函数:从字符串中读取指定格式的数据 头文件: ?
最近,我有一个关于工作预评估的问题,即使查询了每个功能的工作原理,我也不知道如何解决。这是一个伪代码。 下面是一个名为foo()的函数,该函数将被传递一个值并返回一个值。如果将以下值传递给foo函数,
CStr 函数 返回表达式,该表达式已被转换为 String 子类型的 Variant。 CStr(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CSng 函数 返回表达式,该表达式已被转换为 Single 子类型的 Variant。 CSng(expression) expression 参数是任意有效的表达式。 说明 通常,可
CreateObject 函数 创建并返回对 Automation 对象的引用。 CreateObject(servername.typename [, location]) 参数 serv
Cos 函数 返回某个角的余弦值。 Cos(number) number 参数可以是任何将某个角表示为弧度的有效数值表达式。 说明 Cos 函数取某个角并返回直角三角形两边的比值。此比值是
CLng 函数 返回表达式,此表达式已被转换为 Long 子类型的 Variant。 CLng(expression) expression 参数是任意有效的表达式。 说明 通常,您可以使
CInt 函数 返回表达式,此表达式已被转换为 Integer 子类型的 Variant。 CInt(expression) expression 参数是任意有效的表达式。 说明 通常,可
Chr 函数 返回与指定的 ANSI 字符代码相对应的字符。 Chr(charcode) charcode 参数是可以标识字符的数字。 说明 从 0 到 31 的数字表示标准的不可打印的
CDbl 函数 返回表达式,此表达式已被转换为 Double 子类型的 Variant。 CDbl(expression) expression 参数是任意有效的表达式。 说明 通常,您可
CDate 函数 返回表达式,此表达式已被转换为 Date 子类型的 Variant。 CDate(date) date 参数是任意有效的日期表达式。 说明 IsDate 函数用于判断 d
CCur 函数 返回表达式,此表达式已被转换为 Currency 子类型的 Variant。 CCur(expression) expression 参数是任意有效的表达式。 说明 通常,
CByte 函数 返回表达式,此表达式已被转换为 Byte 子类型的 Variant。 CByte(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CBool 函数 返回表达式,此表达式已转换为 Boolean 子类型的 Variant。 CBool(expression) expression 是任意有效的表达式。 说明 如果 ex
Atn 函数 返回数值的反正切值。 Atn(number) number 参数可以是任意有效的数值表达式。 说明 Atn 函数计算直角三角形两个边的比值 (number) 并返回对应角的弧
Asc 函数 返回与字符串的第一个字母对应的 ANSI 字符代码。 Asc(string) string 参数是任意有效的字符串表达式。如果 string 参数未包含字符,则将发生运行时错误。
Array 函数 返回包含数组的 Variant。 Array(arglist) arglist 参数是赋给包含在 Variant 中的数组元素的值的列表(用逗号分隔)。如果没有指定此参数,则
Abs 函数 返回数字的绝对值。 Abs(number) number 参数可以是任意有效的数值表达式。如果 number 包含 Null,则返回 Null;如果是未初始化变量,则返回 0。
FormatPercent 函数 返回表达式,此表达式已被格式化为尾随有 % 符号的百分比(乘以 100 )。 FormatPercent(expression[,NumDigitsAfterD
FormatNumber 函数 返回表达式,此表达式已被格式化为数值。 FormatNumber( expression [,NumDigitsAfterDecimal [,Inc
我是一名优秀的程序员,十分优秀!