如何在c++17中制作一个模板包装器/装饰器
How to make a template Wrapper/Decorator in c++17
尊敬的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
- 如何在c++17中制作一个模板包装器/装饰器
- 将 N-arg 函数包装到另一个函数中
- 包装一个对象并假装它是一个 int
- 如何包装一个函数以适应另一个函数的所需类型
- 包装器是从 strcat_s() 到 strcat() 吗?如果没有,是否可以创建一个?
- 如何将 c++ 类包装到 python 中,以便我可以使用 pybind11 访问其成员的公共方法(成员是一个对象指针)
- 用输出参数包装一个c++函数,以便在javascript/node中使用
- PYBIND11:如何将C 和Python代码包装到一个包装中
- c++11:如何编写一个包装函数来生成"std::function"对象
- OpenCv c++为基本的打印垫函数创建一个c包装器
- Objective-C - 为C++函数创建一个包装器文件
- 用我自己的例外编写一个包装器
- 围绕可以通过C链接器链接的C++库创建一个C包装器
- 如何在Cython中从另一个包装对象返回包装的c++对象
- 如何编写一个包装器,使其保持对输出的跟踪
- 为ActiveMQ制作一个包装器类
- 我如何在c#上为一个包装的c++方法写一个签名,该方法的参数中有一个指向函数的指针
- 为std算法函数创建一个包装器,用容器代替迭代器
- 我可以为任何c++向量做一个C包装器吗?
- 如何为delete编写一个C包装器,既快速又释放给定的任何类型,而不告诉它是什么类型