Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > commenting out 'cout' using preprocessor macro

Reply
Thread Tools

commenting out 'cout' using preprocessor macro

 
 
qazmlp
Guest
Posts: n/a
 
      08-08-2003
I hope comp.lang.c will not find the following question as a
complete off-topic.

I would like to remove ie.comment out the 'cout' statements during
compilation(actually preprocessing) time.


The statements like this:
cout<<"something\n" ;
should be made as
// cout<<"something\n" ;


I tried for the following. But, It doesn't seem to be working.


//--------START
#ifdef DEBUG
#define COUT std::cout
#else
#define COUT \/\/
#endif

int main()
{
COUT<<"HELLO\n"<<std::endl ;
}

//--------END


If you can solve the above problem, please suggest a way for taking
care of
commenting out the 'cout' statements that spans in more than 1 line.
eg:

12 COUT<<"HELLO\n"
13 <<"WORLD\n" ;
 
Reply With Quote
 
 
 
 
Allan Bruce
Guest
Posts: n/a
 
      08-08-2003

"qazmlp" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> I hope comp.lang.c will not find the following question as a
> complete off-topic.
>
> I would like to remove ie.comment out the 'cout' statements during
> compilation(actually preprocessing) time.
>
>
> The statements like this:
> cout<<"something\n" ;
> should be made as
> // cout<<"something\n" ;
>
>
> I tried for the following. But, It doesn't seem to be working.
>
>
> //--------START
> #ifdef DEBUG
> #define COUT std::cout
> #else
> #define COUT \/\/
> #endif
>
> int main()
> {
> COUT<<"HELLO\n"<<std::endl ;
> }
>
> //--------END
>
>
> If you can solve the above problem, please suggest a way for taking
> care of
> commenting out the 'cout' statements that spans in more than 1 line.
> eg:
>
> 12 COUT<<"HELLO\n"
> 13 <<"WORLD\n" ;


This is most definately a c++ question, but the same concept works in c.
The preprocessor directly copies across what you have in a define, so why
not just have
#define COUT //
Allan


 
Reply With Quote
 
 
 
 
Jeff
Guest
Posts: n/a
 
      08-08-2003

"qazmlp" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> I hope comp.lang.c will not find the following question as a
> complete off-topic.
>
> I would like to remove ie.comment out the 'cout' statements during
> compilation(actually preprocessing) time.
>
>
> The statements like this:
> cout<<"something\n" ;
> should be made as
> // cout<<"something\n" ;
>
>


Sorry, it is off-topic here. cout is a part of C++, not C.

[snip]

--
Jeff


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      08-08-2003
"Jeff" <(E-Mail Removed)> wrote...
>
> "qazmlp" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) om...
> > I hope comp.lang.c will not find the following question as a
> > complete off-topic.
> >
> > I would like to remove ie.comment out the 'cout' statements during
> > compilation(actually preprocessing) time.
> >
> >
> > The statements like this:
> > cout<<"something\n" ;
> > should be made as
> > // cout<<"something\n" ;
> >
> >

>
> Sorry, it is off-topic here. cout is a part of C++, not C.


Sorry, Jeff, statements explaining what is off-topic in
comp.lang.c are off-topic in comp.lang.c++. And give "qazmlp"
a break, he expressed his hope, didn't he?

Victor


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      08-08-2003
"Allan Bruce" <(E-Mail Removed)> wrote...
> [...]
> This is most definately a c++ question, but the same concept works in c.


Why is it "definately" a C++ question? C has end-of-line comments.
Depending on how 'cout' is declared, it can be seen as a valid C
construct as well (just as in C++ I can declare it whatever I want
without including the <iostream>)...

> The preprocessor directly copies across what you have in a define, so why
> not just have
> #define COUT //


Why not? Simple. Comments are replaced with a single space char
before macro processing is ever begins. So, the directive you
wrote will be

#define COUT

after the phase 3 of the translation in both languages.

Victor


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      08-08-2003
"Derk Gwen" <(E-Mail Removed)> wrote...
> # //--------START
> # #ifdef DEBUG
> # #define COUT std::cout
> # #else
> # #define COUT \/\/
> # #endif
>
> How about
> #ifdef DEBUG
> #define COUT if (1) std::cout
> #else
> #define COUT if (0) std::cout
> #endif
>
> If you optimise, the unexecutable code after if (0) should be excised.
>
> # 12 COUT<<"HELLO\n"
> # 13 <<"WORLD\n" ;
>
> if (1) std::cout <<"HELLO\n"
> <<"WORLD\n" ;
>
> if (0) std::cout <<"HELLO\n"
> <<"WORLD\n" ;


This approach has a major flaw. Imagine what this will expand into

if (somecondition)
COUT << "HELLO";
else
puts("condition is not met");

This is why it's better to use 'while' for that:

#ifdef WHATEVER
#define COUT std::cout
#else
#define COUT while(0) std::cout
#endif

However, that doesn't address the OP's concern that the code still
remains not compileable by a C compiler. It would be much better
to remove any reference to 'std::cout' whatsoever.

#ifdef __cplusplus
#define COUT std::cout
#else
#define COUT ????
#endif

I don't have a solution. The biggest problem in such case is how
you deal with user-define types that can be output using the C++
shift operator:

SomeUDT udt;
std::cout << udt; // is not uncommon in C++ programs

Making the whole statement invisible to the compiler (after the
preprocessing stage) is the task at hand (or at least how I see
it)...

Victor


 
Reply With Quote
 
Alan Balmer
Guest
Posts: n/a
 
      08-08-2003
On Fri, 8 Aug 2003 11:24:26 -0400, "Victor Bazarov"
<(E-Mail Removed)> wrote:

>"Allan Bruce" <(E-Mail Removed)> wrote...
>> [...]
>> This is most definately a c++ question, but the same concept works in c.

>
>Why is it "definately" a C++ question? C has end-of-line comments.
>Depending on how 'cout' is declared, it can be seen as a valid C
>construct as well (just as in C++ I can declare it whatever I want
>without including the <iostream>)...


I'm curious - just how would you declare 'cout" to make

std::cout<<"HELLO\n"<<std::endl ;

a valid C construct?

--
Al Balmer
Balmer Consulting
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      08-08-2003
"Alan Balmer" <(E-Mail Removed)> wrote...
> On Fri, 8 Aug 2003 11:24:26 -0400, "Victor Bazarov"
> <(E-Mail Removed)> wrote:
>
> >"Allan Bruce" <(E-Mail Removed)> wrote...
> >> [...]
> >> This is most definately a c++ question, but the same concept works in

c.
> >
> >Why is it "definately" a C++ question? C has end-of-line comments.
> >Depending on how 'cout' is declared, it can be seen as a valid C
> >construct as well (just as in C++ I can declare it whatever I want
> >without including the <iostream>)...

>
> I'm curious - just how would you declare 'cout" to make
>
> std::cout<<"HELLO\n"<<std::endl ;
>
> a valid C construct?


I didn't say that the entire statement is a valid construct.
I said 'cout' could be a valid construct. To make 'cout' valid
all you need to do is

int cout;

In the context of the thread there was no requirement to make
'std::cout<<"HELLO\n"<<std::endl ;' a valid C construct.

Victor


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      08-08-2003
"Alan Balmer" <(E-Mail Removed)> wrote...
> On Fri, 8 Aug 2003 13:40:58 -0400, "Victor Bazarov"
> <(E-Mail Removed)> wrote:
>
> >"Alan Balmer" <(E-Mail Removed)> wrote...
> >> On Fri, 8 Aug 2003 11:24:26 -0400, "Victor Bazarov"
> >> <(E-Mail Removed)> wrote:
> >>
> >> >"Allan Bruce" <(E-Mail Removed)> wrote...
> >> >> [...]
> >> >> This is most definately a c++ question, but the same concept works

in
> >c.
> >> >
> >> >Why is it "definately" a C++ question? C has end-of-line comments.
> >> >Depending on how 'cout' is declared, it can be seen as a valid C
> >> >construct as well (just as in C++ I can declare it whatever I want
> >> >without including the <iostream>)...
> >>
> >> I'm curious - just how would you declare 'cout" to make
> >>
> >> std::cout<<"HELLO\n"<<std::endl ;
> >>
> >> a valid C construct?

> >
> >I didn't say that the entire statement is a valid construct.
> >I said 'cout' could be a valid construct. To make 'cout' valid
> >all you need to do is
> >
> > int cout;
> >
> >In the context of the thread there was no requirement to make
> >'std::cout<<"HELLO\n"<<std::endl ;' a valid C construct.
> >
> >Victor
> >

> The context of the thread was that you were contesting the statement
> that the OP's question concerned C++.


No, I wasn't. That's something you just invented. The OP's question
concerns both C++ and C. Off-topicality of the OP's question in
comp.lang.c is what I was contesting.

Victor


 
Reply With Quote
 
Samuele Armondi
Guest
Posts: n/a
 
      08-08-2003
"qazmlp" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> I hope comp.lang.c will not find the following question as a
> complete off-topic.
>
> I would like to remove ie.comment out the 'cout' statements during
> compilation(actually preprocessing) time.
>
>
> The statements like this:
> cout<<"something\n" ;
> should be made as
> // cout<<"something\n" ;
>
>
> I tried for the following. But, It doesn't seem to be working.
>
>
> //--------START
> #ifdef DEBUG
> #define COUT std::cout
> #else
> #define COUT \/\/
> #endif
>
> int main()
> {
> COUT<<"HELLO\n"<<std::endl ;
> }
>
> //--------END
>
>
> If you can solve the above problem, please suggest a way for taking
> care of
> commenting out the 'cout' statements that spans in more than 1 line.
> eg:
>
> 12 COUT<<"HELLO\n"
> 13 <<"WORLD\n" ;


Maybe you could create your own stream and use that as cout, i.e
#if !defined(DEBUG) && defined(__cplusplus)
class MyStream : public std:stream \
{ \
template <class T> \
MyStream& operator << (const T& obj) \
{ return *this; } \
}; \
#define COUT MyStream
#else
#define COUT std::cout
#endif
I'm not sure if the above code will work, I'm especially not too sure about
the template bit. And it most definitely would not work on c systems! Maybe
you could define cout to be an int and the << operator to be + , i.e
#if !defined(DEBUG) && !defined(__cplusplus)
int bogus_cout;
#define COUT bogus_cout=
#define << +
#elif !defined(DEBUG) && defined(__cplusplus)
class MyStream : public std:stream \
{ \
template <class T> \
MyStream& operator << (const T& obj) \
{ return *this; } \
}; \
#define COUT MyStream
#else
#define COUT std::cout
#endif
But you would need to be 100% sure that the code in question does not use
the << _anywhere_. Hope this helps anyway!
S. Armondi



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
java commenting out of code patrick Java 4 01-11-2006 03:05 PM
Compiler error occurred when try to use a flexible template expression in preprocessor definesCompiler error occurred when try to use a flexible template expression in preprocessor defines snnn C++ 6 03-14-2005 04:09 PM
Commenting out VB code =?Utf-8?B?U2FuZHk=?= ASP .Net 5 02-03-2005 05:51 PM
preprocessor, token concatenation, no valid preprocessor token Cronus C++ 1 07-14-2004 11:10 PM
commenting out 'cout' using preprocessor macro qazmlp C Programming 18 08-16-2003 12:59 PM



Advertisments