简体   繁体   中英

What's wrong with this macro?

I am facing a problem with a macro and I can't figure out why.

Here is the macro :

#define WAIT(condition, max_time)               \
   do {                                         \
      int int_loop_wait=0;                      \
      while(1)                                  \    
      {                                         \           
        if(condition) { break; }                \
        sleep(1);                               \
        if(int_loop_wait>=max_time) { break; }  \
        int_loop_wait++;                        \
      }                                         \
    } while(0)                                  \

I got the error

"expected a declaration" line " if(condition) { break; } "

Does anyone understand this error?

The problem is that the backslash followed by a whitespace are together recognized as an escape sequence which in effect cancels out the backslash. Visual C++ 10 even emits error C2017: illegal escape sequence there.

Some of the lines in the code snippet (for example the one with while(1) ) contain one or more whitespaces after backslash. Once the backslashes are treated as escape sequences and removed by the compiler the macro definition gets truncated at that line and the remaining code is compiled as if it doesn't belong to the macro definition.

#define WAIT(condition, max_time)               \
   do {                                         \
      int int_loop_wait=0;                      \
      while(1)                                  \    <<<<<WHITESPACES
      {                                         \<<<this line doesn't belong to macro
        if(condition) { break; }                \<<<and neither does this 
        sleep(1);                               \
        if(int_loop_wait>=max_time) { break; }  \
        int_loop_wait++;                        \
      }                                         \
    } while(0)                                  \

remove the \\ from the last line

I mean change this line

 } while(0)                                                     \

by

 } while(0)

And remove all spaces after the \\

you have some lines which contain spaces after the \\ like:

while(1)                                  \    
      {                                         \           

The culprit is white space after \\ . Removing them will solve.

Macro definition continues if the line ends with \\ , but not with space or any other character.

#define WAIT(condition, max_time)                                  \
   do {                                                            \
      int int_loop_wait=0;                                         \
      while(1){                                                    \
        if(condition) { break; }                                   \
        sleep(1);                                                  \
    if(int_loop_wait>=max_time) { break; }                         \
      }                                                            \
    } while(0)  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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