473,698 Members | 1,947 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

#defining a comment

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
8 2524
On 2006-06-05 17:03, ho********@yaho o.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
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
ho********@yaho o.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
ho********@yaho o.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_OUT PUT and QT_NO_WARNING_O UTPUT during
compilation."

Jun 5 '06 #5
* 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_BF76 7D92_5347_4205_ 8A67_55651CD79C 77_ = true;
#else
static bool nDbgKludge_BF76 7D92_5347_4205_ 8A67_55651CD79C 77_ = false;
#endif

bool const& nDebug = nDbgKludge_BF76 7D92_5347_4205_ 8A67_55651CD79C 77_;

...

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
Yep, indeed I'm using Qt4 (now, I realize it's a fact I should have
mentioned) and defining QT_NO_DEBUG_OUT PUT 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********@yaho o.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_OUT PUT and QT_NO_WARNING_O UTPUT during
compilation."


Jun 5 '06 #7
Jonathan Mcdougall wrote:
ho********@yaho o.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
ho********@yaho o.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1403
by: Brian van den Broek | last post by:
Hi all, IDLE refuses to launch, and I believe it is because I attempted to define a custom key-binding that it doesn't like. I was recently defining a custom keybinding in IDLE 1.1 under Python 2.4 on WinMe. (I was using the simpler of the two binding definition interfaces.) When done, I hit the buttons for 'OK' (or however it is labelled -- I cannot currently check), but the definition dialog remained open. After several tries, I hit...
10
1586
by: Andy B | last post by:
Hi, I am very new to JavaScript and my first assignment is to create a simple game. Below is my code which works fine in IE, but not in any other browser (Opera, Netscape, Mozilla). It has no form defined yet it uses form buttons. Like I said, it works fine in IE but when loaded into another browser and the Start Game! button is clicked, nothing seems to happen. There are no error messages apperaing. Any help would be appreciated. Thanks....
8
1419
by: johny smith | last post by:
If I have a simple class with say a couple of integers only is there any need for me to provide a destructor? thanks!
2
2321
by: Matthew Caesar | last post by:
I've written some code in the C# programming language. I have a lot of calls to a function that prints out some debugging information. I would like to occasionally remove all calls to these functions when I want the program to run quickly (when I'm measuring how fast it runs) but I want the calls to be in when I'm debugging. More detail: I have a class called MyDebug with a member called Trace. I periodically call MyDebug.Trace("string...
2
3529
by: Harry F. Harrison | last post by:
I get 2 compile errors on assembly attributes after creating a custom attribute. If I comment out the attribute, the errors go away. I don't get it because my attribute specifies class usage, not assembly usage. Assembly attribute 'System.Runtime.InteropServices.GuidAttribute' is not valid: Assembly custom attribute 'System.Runtime.InteropServices.GuidAttribute' was specified multiple times with different values Attribute...
38
2091
by: Steven Bethard | last post by:
> >>> aList = > >>> it = iter(aList) > >>> zip(it, it) > > That behavior is currently an accident. >http://sourceforge.net/tracker/?group_id=5470&atid=105470&func=detail&aid=1121416
5
1763
by: chrisstankevitz | last post by:
Why does this code only compile if GLOBAL_IN_STRUCT is defined? It creates a templated class C<T> and defines a global operator* that takes a C<T> on the LHS and a T on the RHS. In the example, T is double, but I call the global operator* with a float. Thanks for your help,
6
1668
by: KK | last post by:
How to define keys in c++?? can anyone help on this?
2
1513
by: emma_middlebrook | last post by:
Hi <ClubNight Name = "Go club" Date = "2006-09-09"> Discussed a few simple proverbs. <Attendee Name = "RJ"/> <Attendee Name = "TT"/> <Attendee Name = "RB"/> </ClubNight> I'm trying to work out if I can actually define this as a complex type
0
8671
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8598
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9152
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9016
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8856
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7709
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4360
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2321
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1997
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.