Search code examples
c++warnings

Explicit mention intended semicolon


Problem

I used an empty statement and am aware of that. So how can I disable the compiler warning?

warning C4390: ';' : empty controlled statement found; is this the intent?

There might be a way of explicitly say that I want an empty statement.

Background

In a development environment the debug function should display the error message and exit the application. In a productive release it should do nothing. So I wrote a macro for that.

#ifdef _DEBUG
#include <iostream>
#define Debug(text) { cout << "Error: " << text; cin.get(); exit(1); }
#else
#define Debug(text) ;
#endif

Solution

  • The common idiom for an empty statement macro is this:

    #define Debug(text) do {} while (0)
    

    As Ben points out in a comment, it is prudent to use this technique in both versions of the macro.

    #ifdef _DEBUG
    #include <iostream>
    #define Debug(text) do { cout << "Error: " << text; cin.get(); exit(1); } while (0)
    #else
    #define Debug(text) do {} while (0)
    #endif
    

    This idiom is discussed in many places. For example: