Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explicit mention intended semicolon

Tags:

c++

warnings

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
like image 866
danijar Avatar asked Oct 20 '25 00:10

danijar


2 Answers

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:

  • http://bruceblinn.com/linuxinfo/DoWhile.html
  • http://yarchive.net/comp/linux/empty_statement_macro.html
like image 143
David Heffernan Avatar answered Oct 21 '25 16:10

David Heffernan


You can explicitly tell the compiler you're not doing anything

(void)0;
like image 44
Motti Avatar answered Oct 21 '25 16:10

Motti