如何在c++17中制作一个模板包装器/装饰器

How to make a template Wrapper/Decorator in c++17

本文关键字:一个 包装 c++17      更新时间:2023-10-16

尊敬的Stackoverflow社区,

我对c++还是有点陌生,我一直在挠头,还没有找到解决问题的方法。我已经搜索和尝试了一段时间,现在我已经到了问一个问题会更有益处和教育意义的地步。

问题:

我想创建一个类或函数,用参数或不用参数包装/装饰给定的函数。就像一个好的老式的@wrappethis在python或c#之类的。

到目前为止,我发现的最接近的东西(优雅、简短且易于使用(来自这个stackoverflow的答案:C++等效装饰器

令人挠头的部分是试图传递一个指针函数。我收到的错误:

错误(活动(E0300指向绑定函数的指针只能用于调用函数

这显然意味着不允许以这种方式传递指针,那么我在这里有什么选择?

一个工作的例子作为答案将是伟大的!

我试图实现的目标示例如下:

Something.h

class Something
{
private:
public:
Something() {}
void v_func_with_nothing() { std::cout << "v_func_with_nothing" << "n"; }
void v_func_with_void(void) { std::cout << "v_func_with_void" << "n"; }
void v_func_with_one_arg(int x) { std::cout << "v_func_with_one_arg" << x << " " << "n"; }
void v_func_with_args(int x, int y) { std::cout << "v_func_with_args" << x << " " << y << "n"; }
int return_func_with_nothing() { return 1; }
int return_func_with_void(void) { return 3; }
int return_func_with_one_arg(int x) { return x; }
int return_func_with_args(int x, int y) { return x+y; }
};

Decorator.h[再次来源:C++等效Decorator]

template<typename T>
auto decorator(T&& func)
{
auto new_function = [func = std::forward<T>(func)](auto&&... args)
{
std::cout << "BEGIN decorating...n";
auto result = func(std::forward<decltype(args)>(args)...);
std::cout << "END decoratingn";
return result;
};
return new_function;
}

main.cpp

#include <iostream>
#include "Something.h"
#include "Decorator.h"
int main()
{
Something* something = new Something();       
auto somedeco = decorator(&something->return_func_with_one_arg);//<-- error here in argument   
//int value = somedeco(**enter an argument**);
//std::cout << value << "n";  
return 0;
}

谢谢!

编辑:解决方案

根据下面给出的善意回答,我想用一个例子来编辑这篇文章。这个问题的解决方案是使用lambda。

Decorator.h:我创建了两个Decorator(一个用于返回函数,一个用于void函数(:

template<typename T>
auto DECO_R(T&& func)
{
try
{
auto new_function = [func = std::forward<T>(func)](auto&&... args)
{
std::cout << "BEGIN RETURN decorating...n";
auto result = func(std::forward<decltype(args)>(args)...);
std::cout << "END RETURN decoratingn";
return result;
};
return new_function;
}
catch (const std::exception& ex)
{
std::cout << ex.what() << "n";
} 
}
template<typename T>
auto DECO_V(T&& func)
{
try
{
auto new_function = [func = std::forward<T>(func)](auto&&... args)
{
std::cout << "BEGIN VOID decorating...n";
func(std::forward<decltype(args)>(args)...);
std::cout << "END VOID decoratingn";
};
return new_function;
}
catch (const std::exception& ex)
{
std::cout << ex.what() << "n";
}
}

Main.cpp:2个示例

int main()
{
Something* something = new Something();

auto somedeco = DECO_R(
[&](int x) {
return something->return_func_with_one_arg(x);
});

int value = somedeco(255);
std::cout << value << "n";
auto some_v_deco = DECO_V(
[&](int x) {
return something->v_func_with_one_arg(x);
});
some_v_deco(2);
return 0;
}

输出

开始返回装饰
结束退货装饰
255

开始VOID装饰
v_func_with_one_arg2
结束VOID装饰

我希望这能帮助其他人。

调用decorator(&something->return_func_with_one_arg)无效。C++中没有指向绑定函数的指针。

例如,如果您希望somedeco是一个类似函数的对象,用于包装对something->return_func_with_one_arg(42)的调用,则需要将调用包装在lambda:中

auto somedeco = decorator(
[&]() {
return something->return_func_with_one_arg(42);
}
);
somedeco();

或者您可以通过decorator传递参数:

auto somedeco = decorator(
[&](int x) {
return something->return_func_with_one_arg(x);
}
);
somedeco(42);

请记住,这将要求something指向的对象比decorator返回的对象寿命更长。

C++中没有简单的1:1替代Python动态类型。您的示例没有编译,因为没有指向某个特定实例的成员函数的指针。指向成员函数的指针总是需要调用一个实例。对于一个简单的情况,我建议使用lambda:

int main() {
Something something;
auto somedeco = [&](auto param) {
// before call
auto v = something.return_func_with_one_arg(param);
// after call
return v;
};
return somedeco(1);
}

正如您所看到的,decorate的整个机制并不是真正需要的,因为使用lamdda可以内联编写封装的函数。另一方面,decorator允许您为不同的方法重用// before call// after call。要修复代码,还可以将lambda传递给decorate

PS:不要使用new来创建对象。

您需要绑定

int main()
{
Something* something = new Something();       

using std::placeholders::_1;
auto f = std::bind( &Something::return_func_with_one_arg, something, _1 );
auto somedeco = decorator( f );//<-- error here in argument   
//int value = somedeco(**enter an argument**);
//std::cout << value << "n";  
return 0;
}

https://godbolt.org/z/zdYW9q