简体   繁体   English

C ++ #define变量参数函数

[英]C++ #define variable parameter function

I have a CPU sensitive application and want to minimize function calls. 我有一个CPU敏感的应用程序,并希望最小化函数调用。 I want to write something like: 我想写一些类似的东西:

#ifdef condition        
#define f(a,b) ff(a,b)   
#define f(a) ff(a)    
#endif    

But the compiler sees f as defined multiple times. 但编译器会将f视为多次定义。 I wanted to use __VAR_ARGS__ but in the example above b is of enum type. 我想使用__VAR_ARGS__但在上面的示例中, benum类型。 Is there a proper way to do it or I should just rename f(a,b) to f2(a,b) ? 有没有正确的方法,或者我应该将f(a,b)重命名为f2(a,b)

To clarify the defines, if active, add calls to functions that process data for printing to file/stdout, otherwise they are replaced with empty lines, so in my opinion this method would improve code size and since the macro is single line keyword like INFO(object->contents) I think it's more readable. 为了阐明定义,如果激活,则添加对处理数据以打印到文件/标准输出的函数的调用,否则它们将被替换为空行,因此在我看来这种方法会改善代码大小,因为宏是单行关键字,如INFO(object->contents)我认为它更具可读性。 Also it would have been useful if I could have added something like WARN("message") and WARN("value is",obj->printvalue()) . 如果我可以添加诸如WARN("message")WARN("value is",obj->printvalue())那将会非常有用。

I also think inline would do the trick (from the answer below). 我也认为inline做到这一点(从下面的答案)。

This is a very C-ish way of approaching this. 这是一种非常接近这种方式的C-ish方式。 Simply make it an overloaded inline function. 简单地使它成为一个重载的inline函数。 Any optimiser worthy of the name will inline the call. 任何名副其实的优化器都会内联这个电话。

My first guess is that you are optimizing in the wrong areas. 我的第一个猜测是你在错误的领域进行优化。 Good compilers will optimize in this case. 在这种情况下,优秀的编译器将优化。 Obfuscating code will make it harder for the compiler to do so. 混淆代码会使编译器更难以这样做。

Found the answer from the c++ book: 从c ++书中找到答案:
Macro names cannot be overloaded: 宏名称不能重载:
#define PRINT(a ,b ) cout <<(a )<<(b )
#define PRINT (a ,b ,c ) cout <<(a )<<(b )<<(c ) /* trouble?: redefines, does not overload */

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

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