 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
lotusny78@yahoo.com Guest
|
Posted: Thu Apr 21, 2005 4:50 pm Post subject: const bool as debugging switch |
|
|
In "C++ Gotchas," Stephen Dewhurst says that using:
const bool debug = ...;
is preferable to #ifdefs because:
1) all modern compilers should be able to easily optimize it away
2) all the code in your project will be parsed and checked, not
just that of the current configuration
He also suggests using a "makefile or similar facility" to choose
between debug and production versions, instead of:
const bool debug =
#ifdef NDEBUG
false
#else
true
#endif
;
1) How would you apply the "makefile or similar facility" part ( I use
VC++, I don't think I've ever even seen a makefile )?
2) Does anyone have any positive/negative experiences with this
technique?
Thanks everybody.
- Sean
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
mlimber Guest
|
Posted: Fri Apr 22, 2005 2:26 pm Post subject: Re: const bool as debugging switch |
|
|
In VC++ 6.0 you can generate a makefile for your project from the
project menu (of course, you have to use a compatible make utility to
use that file).
Alternately, you can specify a preprocessor definition in the project
settings (Project | Settings... | C/C++ | Preprocessor). If you look
there, you might see something like:
WIN32,_DEBUG
to which you can add your variable and its value:
WIN32,_DEBUG,MY_DEBUG_ENABLE=true
which can be used in the code thusly:
const bool debug = MY_DEBUG_ENABLE; // Defined in the project settings
Cheers!
M
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Daniel Krügler (ne Spange Guest
|
Posted: Sat Apr 23, 2005 1:59 am Post subject: Re: const bool as debugging switch |
|
|
Hello Sean,
[email]lotusny78 (AT) yahoo (DOT) com[/email] schrieb:
| Quote: | In "C++ Gotchas," Stephen Dewhurst says that using:
const bool debug = ...;
is preferable to #ifdefs because:
1) all modern compilers should be able to easily optimize it away
2) all the code in your project will be parsed and checked, not
just that of the current configuration
He also suggests using a "makefile or similar facility" to choose
between debug and production versions, instead of:
const bool debug =
#ifdef NDEBUG
false
#else
true
#endif
;
1) How would you apply the "makefile or similar facility" part ( I use
VC++, I don't think I've ever even seen a makefile )?
|
Poor child .
I don't know any IDE which does not allow providing preprocessor
definitions. Note that by default
the VC++ environment will define NDEBUG for you in release builds and
not so in debug builds (compliant
to the above test).
| Quote: | 2) Does anyone have any positive/negative experiences with this
technique?
|
Positive, definitly, although I also use the ol' way via std::assert.
The big advantage is, that you can
use template techniques delegating to configuration-dependent code.
The simplest way is something like:
template <bool Debug>
struct Checker {
static void test(Args args) {
// Perform some debug tests using arguments
}
};
template <>
struct Checker<false> {
static void test(Args) {} // Do nothing
};
// Usage:
void foo(Args args) {
Checker<debug>::test(args);
}
More advanced techniques can use the template pattern or similar things
to perform
prefix/suffix code tests.
Greetings from Bremen,
Daniel Krügler
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
lotusny78@yahoo.com Guest
|
Posted: Sat Apr 23, 2005 5:20 pm Post subject: Re: const bool as debugging switch |
|
|
Of course you can #define through the IDE, but the point from the book
is that there is a way to use the makefile so that you don't have any
conditional compilations in your code. But how?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ben Hutchings Guest
|
Posted: Sun Apr 24, 2005 9:25 am Post subject: Re: const bool as debugging switch |
|
|
[email]lotusny78 (AT) yahoo (DOT) com[/email] wrote:
| Quote: | Of course you can #define through the IDE, but the point from the book
is that there is a way to use the makefile so that you don't have any
conditional compilations in your code. But how?
|
Put conditional code in separate source files, and then select
different sets of source files in the makefile depending on make
variables. For example, if you want to build Linux and Windows
versions of an app with some shared code and some platform-dependent,
you could use:
sources := foo-common.cpp bar.cpp
ifeq ($(TARGET),Linux)
sources += foo-linux.cpp baz-linux.cpp
endif
ifeq ($(TARGET),Windows)
sources += foo-win32.cpp baz-win32.cpp
endif
objects := $(sources:.cpp=.o)
app : $(objects)
$(CXX) $(CXXFLAGS) $(LDFLAGS) $< -o $@
Then it's just a case of "make TARGET=Linux" or "make TARGET=Windows".
(Some of the above might require GNU make. I don't bother trying to
use other versions.)
If you use an IDE you may be able to exclude certain source files
from certain build configurations. Visual Studio certainly provides
this option.
--
Ben Hutchings
Having problems with C++ templates? Your questions may be answered by
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Walter Guest
|
Posted: Sun Apr 24, 2005 8:59 pm Post subject: Re: const bool as debugging switch |
|
|
<lotusny78 (AT) yahoo (DOT) com> wrote
| Quote: | In "C++ Gotchas," Stephen Dewhurst says that using:
const bool debug = ...;
is preferable to #ifdefs because:
1) all modern compilers should be able to easily optimize it away
2) all the code in your project will be parsed and checked, not
just that of the current configuration
|
The debug constant can deal with expressions and statements, but not
declarations. How would one conditionally add a member to a class with this?
It won't work.
Equally bad, the debug expressions must still undergo semantic analysis, and
be semantically correct. This precludes any referencing of conditionally
compiled debug declarations.
-Walter
www.digitalmars.com C, C++, D compilers
"code of the nerds"
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Tue Apr 26, 2005 9:19 am Post subject: Re: const bool as debugging switch |
|
|
[email]lotusny78 (AT) yahoo (DOT) com[/email] wrote:
| Quote: | Of course you can #define through the IDE, but the point from
the book is that there is a way to use the makefile so that
you don't have any conditional compilations in your code. But
how?
|
bool const debugOn = DEBUGON ;
Compiled with either -DDEBUGON=true or -DDEBUGON=false.
To be frank, however, I don't quite see the interest in the
technique. It's only formally that you don't have condition
compilation; practically, the effects are the same. Maybe even
worse -- if I see #ifdef, I know that what follows may not be a
part of the code; if I see if ( debugOn ), it's less obvious.
On the whole, however, I'd avoid either.
--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Allan W Guest
|
Posted: Wed Apr 27, 2005 8:38 am Post subject: Re: const bool as debugging switch |
|
|
k... (AT) gabi-soft (DOT) fr wrote:
| Quote: | bool const debugOn = DEBUGON ;
Compiled with either -DDEBUGON=true or -DDEBUGON=false.
To be frank, however, I don't quite see the interest in the
technique. It's only formally that you don't have condition
compilation; practically, the effects are the same. Maybe even
worse -- if I see #ifdef, I know that what follows may not be a
part of the code; if I see if ( debugOn ), it's less obvious.
|
If I see if(debugOn), I assume that debugOn is something that can
be set by the user during a debugging session. Maybe the user
has detected a problem on iteration number 4000 through a loop,
so they enable some log messages starting on iteration 3998
to see what's different on the 4000th time through.
My point is that I wouldn't expect debugOn to be immutable.
Most debuggers can alter const objects. I'd declare it volatile
though, just to make sure that the compiler doesn't optimize
out the part after if(debugOn)... (not an issue if optimizations
have been disabled).
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
James Kanze Guest
|
Posted: Wed May 04, 2005 8:01 am Post subject: Re: const bool as debugging switch |
|
|
Allan W wrote:
| Quote: | k... (AT) gabi-soft (DOT) fr wrote:
bool const debugOn = DEBUGON ;
Compiled with either -DDEBUGON=true or -DDEBUGON=false.
To be frank, however, I don't quite see the interest in the
technique. It's only formally that you don't have condition
compilation; practically, the effects are the same. Maybe
even worse -- if I see #ifdef, I know that what follows may
not be a part of the code; if I see if ( debugOn ), it's less
obvious.
If I see if(debugOn), I assume that debugOn is something that
can be set by the user during a debugging session. Maybe the
user has detected a problem on iteration number 4000 through a
loop, so they enable some log messages starting on iteration
3998 to see what's different on the 4000th time through.
|
I totally agree there (although for filtering log messages, grep
and akw are pretty powerful tools). But in the example, debugOn
is declared const.
| Quote: | My point is that I wouldn't expect debugOn to be immutable.
Most debuggers can alter const objects.
|
Which probably won't help. Typically, if you compile with
debugOn false, the compiler won't even generate the code for the
unexecuted branches.
And of course, if you've instrumented the code, why on earth
would you bother with a debugger:-)?
| Quote: | I'd declare it volatile though, just to make sure that the
compiler doesn't optimize out the part after
if(debugOn)... (not an issue if optimizations have been
disabled).
|
If I saw a bool const volatile, I'd immediately suppose that it
must be memory mapped IO.
--
James Kanze mailto: [email]james.kanze (AT) free (DOT) fr[/email]
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 pl. Pierre Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
billvanvalk@no_spam_gmail Guest
|
Posted: Wed May 04, 2005 11:57 pm Post subject: Re: const bool as debugging switch |
|
|
const is advisatory in C++. Can't one still do the following in some
module where this variable is visible:
*(bool*)&debug = false;
So can the compiler really optimize this code away?
It is a global variable, how does the compiler know that debug's value
has not been changed in some other module other than that which it is
currently compiling?
BillV
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ben Hutchings Guest
|
Posted: Tue May 10, 2005 2:27 pm Post subject: Re: const bool as debugging switch |
|
|
[email]billvanvalk (AT) no_spam_gmail_no_spam (DOT) com[/email] wrote:
| Quote: |
const is advisatory in C++.
|
No. A const-qualified object must never be modified (with the
exception of any mutable members it may have).
| Quote: | Can't one still do the following in some module where this variable
is visible:
*(bool*)&debug = false;
|
One can try that, but it has undefined behaviour.
| Quote: | So can the compiler really optimize this code away?
|
Yes.
| Quote: | It is a global variable, how does the compiler know that debug's value
has not been changed in some other module other than that which it is
currently compiling?
|
There is no legal way to do that, so it is allowed to assume that it
doesn't.
--
Ben Hutchings
Having problems with C++ templates? Your questions may be answered by
<http://womble.decadentplace.org.uk/c++/template-faq.html>.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|