我需要一个宏/模板函数来包装特定对象上某些方法的函数调用,即
a.Destroy()
其中 a 可以是任何类型,Destroy 和 Destroy 可能有也可能没有 0 到 n 个参数。在这个包装器中,我需要做一些检查。
我希望能够将此函数作为包装器调用:
DESTROY_CHECK(a.Destroy(p1,p2,...))
或
DESTROY_CHECK(a, Destroy(p1,p2,...))
我怎样才能做到这一点?
最佳答案
可以考虑variadic macro :
#define identifier( parameters, ... ) replacement-list
... defines a function-like macro with variable number of arguments. The additional arguments can be accessed using
__VA_ARGS__
identifier, which is then replaced with arguments, supplied with the identifier to be replaced.
#define DESTROY_CHECK(x, ...) assert(x.Destroy(__VA_ARGS__))
关于c++ - Variadic 模板包装函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40509694/