使用 Lambda/Template/SFINAE 自动尝试/捕获保护蹦床功能
Using Lambda/Template/SFINAE to automate try/catch-safeguarding of trampoline functions
我有100个左右的蹦床函数。我想知道是否可以自动将每个包装在 try/catch 块中。
请提前警告,这不是一个容易的问题。我将首先用(简化的)代码描述问题,然后尝试在下面尽我所能回答它,以便读者可以看到我所处的位置。
Foo有一个函数指针表:
编辑:这是一个 C 函数指针表。 所以它可以接受static W::w
.
签名在这里: http://svn.python.org/projects/python/trunk/Include/object.h
编辑:我在这里尝试了一个测试用例:
class Foo {
Table table;
Foo() {
// Each slot has a default lambda.
:
table->fp_53 = [](S s, A a, B b) -> int {cout<<"load me!";};
table->fp_54 = [](S s, C c, D d, E e) -> float {cout<<"load me!";};
// ^ Note: slots MAY have different signatures
// only the first parameter 'S s' is guaranteed
}
// Foo also has a method for loading a particular slot:
:
void load53() { table->fp_53 = func53; }
void load54() { table->fp_54 = func54; }
:
}
如果一个特定的插槽被"加载",这就是加载到其中的内容:
int func53(S s, A a, B b) {
try{
return get_base(s)->f53(a,b);
}
catch(...) { return 42;}
}
float func54(S s, C c, D d, E e) {
try{
return get_base(s)->f54(c,d,e);
}
catch(...) { return 3.14;}
}
我正在尝试使用 lambda 来实现这一点,以便绕过必须单独定义所有这些func53
。 像这样:
class Foo {
:
void load53() {
table->fp_53 =
[](S s, A a, B b)->int { return get_base(s)->f53(a,b); }
}
void load54() {
table->fp_54 =
[](S s, C c, D d, E e)->float { return get_base(s)->f54(c,d,e); }
}
但是,这无法捕获错误。我需要在返回语句周围进行尝试/捕获:
try{ return get_base(s)->f53(a,b); } catch{ return 42; }
但是,这会产生很多混乱。如果我能做到,那就太好了:
return trap( get_base(s)->f53(a,b); )
我的问题是:有没有办法编写这个trap
函数(不使用 #define)?
这是我到目前为止想出的:
我认为这将传递所有必要的信息:
trap<int, &Base::f53>(s,a,b)
Trap的定义可能如下所示:
template<typename RET, Base::Func>
static RET
trap(S s, ...) {
try {
return get_base(s)->Func(...);
}
catch {
return std::is_integral<RET>::value ? (RET)(42) : (RET)(3.14);
}
}
这可能允许一个非常干净的语法:
class Foo {
:
void load53() { table->fp_53 = &trap<int, &Base::f53>; }
void load54() { table->fp_54 = &trap<float, &Base::f54>; }
}
在这一点上,我甚至不确定是否违反了某些法律。 table->fp_53
必须是有效的 C 函数指针。
传入非静态成员函数(&Base::f53>
)的地址不会违反这一点,因为它是一个模板参数,不会影响trap
的签名
同样,...
应该没问题,因为 C 允许 varargs。
那么,如果这确实有效,可以清理吗?
我的想法是:
1)也许...应作为包移回模板参数。
2)也许可以推断出trap的返回类型,并保存一个模板参数
3)Base::Func
模板参数是非法语法。我怀疑它甚至没有接近合法的东西。这可能会破坏整个方法。
#include <utility>
template <typename T, T t>
struct trap;
template <typename R, typename... Args, R(Base::*t)(Args...)>
struct trap<R(Base::*)(Args...), t>
{
static R call(int s, Args... args)
{
try
{
return (get_base(s)->*t)(std::forward<Args>(args)...);
}
catch (...)
{
return std::is_integral<R>::value ? static_cast<R>(42)
: static_cast<R>(3.14);
}
}
};
用法:
table->fp_53 = &trap<decltype(&Base::f53), &Base::f53>::call;
table->fp_54 = &trap<decltype(&Base::f54), &Base::f54>::call;
演示
注意:std::forward
仍然可以使用,尽管Args
本身不是转发引用。
template<typename RET, typename... Args>
struct trap_base {
template<RET (Base::* mfptr)(Args...)>
static RET
trap(S s, Args... args) {
try {
return (get_base(s).*mfptr)(args...);
}
catch (...) {
return std::is_integral<RET>::value ? (RET)(42) : (RET)(3.14);
}
}
};
用法:
void load53() { table.fp_53 = &trap_base<int, int>::trap<&Base::f53>; }
void load54() { table.fp_54 = &trap_base<float, int, float>::trap<&Base::f54>; }
演示。
您可能还可以使用部分专用化从decltype(&base::f53)
中提取RET
和Args
。
trap_gen
是一个函数,它返回指向动态生成的函数的函数指针,相当于trap
函数。
这是你如何使用它
table->fp_53 = trap_gen<>(Base::f53);
table->fp_54 = trap_gen<>(Base::f54);
...
其中Base::f53
和Base::f54
是静态成员函数(或函数指针,或命名空间中的全局函数)。
概念验证 :
#include <iostream>
template<typename R, class...A>
R (*trap_gen(R(*f)(A...)))(A...)
{
static auto g = f;
return [](A... a)
{
try {
return g(a...);
} catch (...) {
return std::is_integral<R>::value ? static_cast<R>(42)
: static_cast<R>(3.14);
}
};
}
int add(int a, int b)
{
return a+b;
}
int main() {
int(*f)(int, int) = trap_gen<>(add);
std::cout << f(2, 3) << std::endl;
return 0;
}
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- 多态性和功能结合
- 带内存和隔离功能的SQLite
- 有充分的理由在h文件中使用include保护而不是cpp文件吗
- 为什么在保护模式下继承升级不起作用
- 在CMakeLists.txt的安装功能中使用.cmake文件有什么用
- 覆盖私有功能,它与受保护功能有何不同?
- 如何创建功能以用受保护或私人侵犯的对象在对象内部操纵对象
- 如何通过运算符<<的重载访问受保护的功能?C++
- 好友功能和受保护的数据
- 使用具有虚拟功能的受保护或公共功能
- 如果我希望子类向父类添加功能,我是否应该保护我的属性
- 受保护成员辅助功能额外签入C++的原因是什么?
- 使用 Lambda/Template/SFINAE 自动尝试/捕获保护蹦床功能
- 使用创建文件功能创建文件会导致写保护文件
- 错误:..受保护的功能
- 问如何正确地做一个屏幕保护程序的功能窗口C++
- 由define保护的文档功能
- 为什么 C/C++ 没有内置包含保护功能?
- 虚拟保护功能错误