视觉工作室2015中的宏:预期表达式[重复](macro in visual studio 2015: expected an expression [duplicate])

这个问题在这里已有答案:

print_once,它是如何阻塞的? 1个答案

我有这个代码

// show_time.c #include <time.h> #include <stdio.h> #define _timer() ({((double)(clock()))/CLOCKS_PER_SEC;}) int main() { int i, j; double timer = _timer(); for (i = 0; i < 1000; i++) for (j = 0; j < 10000; j++) i*j; printf("%f", _timer() - timer); return 0; }

随着gcc工作正常,我有时间。 但是在Visual Studio 2015中, _timer被标记,我得到了expected an expression的消息,

This question already has an answer here:

print_once, how blockwise it is working? 1 answer

I have this code

// show_time.c #include <time.h> #include <stdio.h> #define _timer() ({((double)(clock()))/CLOCKS_PER_SEC;}) int main() { int i, j; double timer = _timer(); for (i = 0; i < 1000; i++) for (j = 0; j < 10000; j++) i*j; printf("%f", _timer() - timer); return 0; }

with gcc work fine, I get the time. But in Visual Studio 2015, _timer is marked and I get the message expected an expression,

最满意答案

我不知道为什么它适用于gcc(现在我知道在关注重复链接后),但它确实过于复杂。 更简单的表达式也是如此,显然更具可移植性。

所以放下花括号和分号。

那可行:

#define _timer() ((double)(clock())/CLOCKS_PER_SEC)

或创建一个真正的功能

double _timer() { return ((double)(clock()))/CLOCKS_PER_SEC; }

但不是两种结构的混合

I don't know why it works with gcc (now I know after following the duplicate link), but it's really overcomplex. A simpler expression does the same and is obviously more portable.

So drop the curly braces and the semi-colon.

That will work:

#define _timer() ((double)(clock())/CLOCKS_PER_SEC)

or create a real function

double _timer() { return ((double)(clock()))/CLOCKS_PER_SEC; }

but not a mix of the 2 constructs

更多推荐