By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,114 Members | 1,574 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,114 IT Pros & Developers. It's quick & easy.

#defining a comment

P: n/a
Hi everybody!

This is basically a pre-processor doubt. I have this very simple
define:

#ifdef NDEBUG
#define QTRACE //
#else
#define QTRACE qDebug()
#endif

.... so that the line:

QTRACE << "Foi lido o valor" << *it;

.... is expanded to:

qDebug() << "Foi lido o valor" << *it;

.... in debug mode, and is expanded to:

// << "Foi lido o valor" << *it;

.... in release mode. Unfortunately compilation yields:

main.cpp(17) : error C2143: syntax error : missing ';' before '<<'

.... Meaning the pre-processor expanded the macro to absolutely nothing
:( Does anyone know a way to overcome this? I'm developing this on
Visual Studio .NET 2003 but it will run on a HP-UX system, so I can't
rely on any Microsoft extension.

Thanks in advance,

André

Jun 5 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
On 2006-06-05 17:03, ho********@yahoo.com wrote:
Hi everybody!

This is basically a pre-processor doubt. I have this very simple
define:

#ifdef NDEBUG
#define QTRACE //
#else
#define QTRACE qDebug()
#endif

... so that the line:

QTRACE << "Foi lido o valor" << *it;

... is expanded to:

qDebug() << "Foi lido o valor" << *it;

... in debug mode, and is expanded to:

// << "Foi lido o valor" << *it;

... in release mode. Unfortunately compilation yields:

main.cpp(17) : error C2143: syntax error : missing ';' before '<<'

... Meaning the pre-processor expanded the macro to absolutely nothing
:( Does anyone know a way to overcome this? I'm developing this on
Visual Studio .NET 2003 but it will run on a HP-UX system, so I can't
rely on any Microsoft extension.


I'm not really sure since I seldom use macros but I think that the // is
treated as a comment by the preprocessor. Try #define QTRACE \/\/.

Erik Wikström
--
"I have always wished for my computer to be as easy to use as my
telephone; my wish has come true because I can no longer figure
out how to use my telephone" -- Bjarne Stroustrup
Jun 5 '06 #2

P: n/a
Erik Wikström wrote:
#ifdef NDEBUG
#define QTRACE //
#else
#define QTRACE qDebug()
#endif
I'm not really sure since I seldom use macros but I think that the // is
treated as a comment by the preprocessor. Try #define QTRACE \/\/.


I accidentally did that once. (The comment addressed the macro, not
deliberately the rest of the line.)

IIRC VC++ did not erase the following text, and GNU g++ did.

I don't care what the Standard sez - don't do it.

André is advised to do this:

#define QTRACE(x) qDebug() << x_

QTRACE("Foi lido o valor" << *it);

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Jun 5 '06 #3

P: n/a
ho********@yahoo.com wrote:
Hi everybody!

This is basically a pre-processor doubt. I have this very simple
define:

#ifdef NDEBUG
#define QTRACE //
#else
#define QTRACE qDebug()
#endif

... so that the line:

QTRACE << "Foi lido o valor" << *it;

... is expanded to:

qDebug() << "Foi lido o valor" << *it;

... in debug mode, and is expanded to:

// << "Foi lido o valor" << *it;

... in release mode. Unfortunately compilation yields:

main.cpp(17) : error C2143: syntax error : missing ';' before '<<'


I seem to remember from Herb Sutter's Exception C++ that comments
cannot be created with macros. When the preprocessor gets to macros, it
has already processed the comments. Unfortunately, I cannot verify this
right now.
Jonathan

Jun 5 '06 #4

P: n/a
ho********@yahoo.com wrote:
Hi everybody!

This is basically a pre-processor doubt. I have this very simple
define:

#ifdef NDEBUG
#define QTRACE //
#else
#define QTRACE qDebug()
#endif

... so that the line:

QTRACE << "Foi lido o valor" << *it;

... is expanded to:

qDebug() << "Foi lido o valor" << *it;

... in debug mode, and is expanded to:

// << "Foi lido o valor" << *it;

... in release mode. Unfortunately compilation yields:

main.cpp(17) : error C2143: syntax error : missing ';' before '<<'

... Meaning the pre-processor expanded the macro to absolutely nothing
:( Does anyone know a way to overcome this? I'm developing this on
Visual Studio .NET 2003 but it will run on a HP-UX system, so I can't
rely on any Microsoft extension.


If you're using Qt4, this quote from the documentation might be interesting
for you:

"Both qDebug() and qWarning() are debugging tools. They can be compiled away
by defining QT_NO_DEBUG_OUTPUT and QT_NO_WARNING_OUTPUT during
compilation."

Jun 5 '06 #5

P: n/a
* Phlip:
Erik Wikström wrote:
#ifdef NDEBUG
#define QTRACE //
#else
#define QTRACE qDebug()
#endif

I'm not really sure since I seldom use macros but I think that the // is
treated as a comment by the preprocessor. Try #define QTRACE \/\/.


I accidentally did that once. (The comment addressed the macro, not
deliberately the rest of the line.)

IIRC VC++ did not erase the following text, and GNU g++ did.

I don't care what the Standard sez - don't do it.

André is advised to do this:

#define QTRACE(x) qDebug() << x_

QTRACE("Foi lido o valor" << *it);


Were it not for modern compilers' annoying habit of spewing out warnings
for constant boolean expressions & dead code, an alternative could be

#ifdef NDEBUG
bool const nDebug = true;
#else
bool const nDebug = false;
#endif

...

nDebug || qDebug() << "Foi lido o valor" << *it;

Possibly the sillywarnings can be circumvented by doing instead

#ifdef NDEBUG
static bool nDbgKludge_BF767D92_5347_4205_8A67_55651CD79C77_ = true;
#else
static bool nDbgKludge_BF767D92_5347_4205_8A67_55651CD79C77_ = false;
#endif

bool const& nDebug = nDbgKludge_BF767D92_5347_4205_8A67_55651CD79C77_;

...

nDebug || qDebug() << "Foi lido o valor" << *it;

Hah, now that I thunk of it I think I'll try it, perhaps it works... ;-)

It (1) isn't a macro, which is Good, and (2) spells out exactly what the
effect is each place it's used, which is also Good, and (3) the output
expression isn't evaluated (no side-effects) if nDebug, which is both
Good and Bad (Bad because release and debug builds may act differently),
but is the same as with the macro solution.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jun 5 '06 #6

P: n/a
Yep, indeed I'm using Qt4 (now, I realize it's a fact I should have
mentioned) and defining QT_NO_DEBUG_OUTPUT indeed solves the problem.
Stuff to be outputted doesn't even get to be evaluated, from the tests
I've done, which is exactly what I wanted.

Thanks Rolf and everybody else!

André

Rolf Magnus escreveu:
ho********@yahoo.com wrote:
Hi everybody!

This is basically a pre-processor doubt. I have this very simple
define:

#ifdef NDEBUG
#define QTRACE //
#else
#define QTRACE qDebug()
#endif

... so that the line:

QTRACE << "Foi lido o valor" << *it;

... is expanded to:

qDebug() << "Foi lido o valor" << *it;

... in debug mode, and is expanded to:

// << "Foi lido o valor" << *it;

... in release mode. Unfortunately compilation yields:

main.cpp(17) : error C2143: syntax error : missing ';' before '<<'

... Meaning the pre-processor expanded the macro to absolutely nothing
:( Does anyone know a way to overcome this? I'm developing this on
Visual Studio .NET 2003 but it will run on a HP-UX system, so I can't
rely on any Microsoft extension.


If you're using Qt4, this quote from the documentation might be interesting
for you:

"Both qDebug() and qWarning() are debugging tools. They can be compiled away
by defining QT_NO_DEBUG_OUTPUT and QT_NO_WARNING_OUTPUT during
compilation."


Jun 5 '06 #7

P: n/a
Jonathan Mcdougall wrote:
ho********@yahoo.com wrote:
Hi everybody!

This is basically a pre-processor doubt. I have this very simple
define:

#ifdef NDEBUG
#define QTRACE //
#else
#define QTRACE qDebug()
#endif

... so that the line:

QTRACE << "Foi lido o valor" << *it;

... is expanded to:

qDebug() << "Foi lido o valor" << *it;

... in debug mode, and is expanded to:

// << "Foi lido o valor" << *it;

... in release mode. Unfortunately compilation yields:

main.cpp(17) : error C2143: syntax error : missing ';' before '<<'
I seem to remember from Herb Sutter's Exception C++ that comments


That's "More Exceptional C++", sorry Herb.
cannot be created with macros. When the preprocessor gets to macros, it
has already processed the comments. Unfortunately, I cannot verify this
right now.


The book talks about 2.1 in the C++ standard which states

"2.1 Phases of translation

The precedence among the syntax rules of translation is specified by
the following phases.

§1 [...]
§2 [...]
§3 [...] Each comment is replaced by one space character. [...]
§4 Preprocessing directives are executed and macro invocations are
expanded. [...]"

So it is impossible to create a comment with a macro. Sutter says

"A published article once claimed that it's possible for a macro to
create a comment as follows:

#define COMMENT SLASH(/)
#define SLASH(s) /##s

This is nonstandard and not portable, but it's an understandable
mistake because it actually works on some popular compilers. Why does
it work? Because those compilers don't implement the phases of
translation correctly."
Jonathan

Jun 5 '06 #8

P: n/a
ho********@yahoo.com wrote:
Hi everybody!

This is basically a pre-processor doubt. I have this very simple
define:

#ifdef NDEBUG
#define QTRACE //
#else
#define QTRACE qDebug()
#endif

... so that the line:

QTRACE << "Foi lido o valor" << *it;

... is expanded to:

qDebug() << "Foi lido o valor" << *it;

... in debug mode, and is expanded to:

// << "Foi lido o valor" << *it;

... in release mode. Unfortunately compilation yields:

main.cpp(17) : error C2143: syntax error : missing ';' before '<<'

... Meaning the pre-processor expanded the macro to absolutely nothing
:( Does anyone know a way to overcome this? I'm developing this on
Visual Studio .NET 2003 but it will run on a HP-UX system, so I can't
rely on any Microsoft extension.

Thanks in advance,

André


I recomend you use the following method:
#ifdef NDEBUG
#define QTRACE if (1);else qDebug()
#else
#define QTRACE qDebug()
#endif

Most compilers will optimize away the No-Debug QTRACE.

When using this method, it's important to use if(1);else instead of if
(0)
The reason you want to do this is to avoid incorrect logic flow with
code like the following:

if (foofoo == 0 )
QTRACE("Error: No foofoo");
else
printf("We have some foofoo.");

By using an if(1);else in the macro, the above printf will still get
called if foofoo is not zero.

Jun 5 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.