有没有办法像 Python 风格一样在 C++ 中装饰函数或方法?
@decorator
def decorated(self, *args, **kwargs):
pass
例如使用宏:
DECORATE(decorator_method)
int decorated(int a, float b = 0)
{
return 0;
}
或者
DECORATOR_MACRO
void decorated(mytype& a, mytype2* b)
{
}
可能吗?
原文由 Artem Selivanov 发布,翻译遵循 CC BY-SA 4.0 许可协议
std::function
为我提出的解决方案提供了大部分构建块。这是我提出的解决方案。
输出:
附言
Decorator
提供了一个地方,您可以在其中添加除进行函数调用之外的功能。如果您想简单地传递到std::function
,您可以使用: