简体   繁体   English

可变参数模板包装函数调用

[英]Variadic template wrapping function call

I need a macro/templated function that will wrap function call of some method on specific object, ie 我需要一个宏/模板函数,该函数将在特定对象上包装某些方法的函数调用,即

a.Destroy()

where a can be of any type as well as Destroy and Destroy may or may not take 0 to n parameters. 其中a可以是任何类型,并且Destroy和Destroy可以或可以不使用0到n参数。 Inside this wrapper I need to do some checks. 在这个包装器内,我需要做一些检查。

I would like to be able to call this function as a wrapper: 我希望能够将此函数作为包装器调用:

DESTROY_CHECK(a.Destroy(p1,p2,...))

or 要么

DESTROY_CHECK(a, Destroy(p1,p2,...))

How can I achieve this? 我该如何实现?

You can consider 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. 可以使用__VA_ARGS__标识符访问其他参数,然后将其替换为随替换标识符一起提供的参数。

#define DESTROY_CHECK(x, ...) assert(x.Destroy(__VA_ARGS__))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM