#defining a comment

H

hobbes_7_8

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é
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

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
 
P

Phlip

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);
 
J

Jonathan Mcdougall

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
 
R

Rolf Magnus

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."
 
A

Alf P. Steinbach

* Phlip:
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.
 
H

hobbes_7_8

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:
 
J

Jonathan Mcdougall

Jonathan said:
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
 
A

Axter

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,816
Messages
2,569,710
Members
45,498
Latest member
SharylPont

Latest Threads

Top