 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Srik Guest
|
Posted: Wed Nov 10, 2004 4:25 pm Post subject: templates for compile time switches |
|
|
I am tryng to figure out if I could
reduce "#ifdef ...#else" clutter in the source code.
(I have dozens of such symbols defined from various sources
and hence the query)
Here is an example:
--------------------------------
#define DEBUG_PRINT
#ifdef DEBUG_PRINT
const bool bDebugPrint = true;
#else
const bool bDebugPrint = false;
#endif
template<bool T_cond> class CCondExec {
public:
CCondExec(void (*f)(void)) {}
inline void operator()() {}
}; // template<bool T_cond> class CCondExec {
template<> class CCondExec<true> {
void (*m_func)(void);
public:
CCondExec(void (*f)(void)) { m_func = f; }
inline void operator()() { m_func(); }
}; // template<bool T_cond> class CCondExec {
void debugPrint() {
cout << "debugPrint() called" << endl;
}
int main(int argc, char *argv[])
{
static CCondExec
condExec();
}
----------------------
This calls debugPrint() only if DEBUG_PRINT is defined.
Otherwise the call is a no-op.
My query is concerning efficiency of this approach.
Is there a better way?
-srik
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
jakacki Guest
|
Posted: Tue Nov 16, 2004 11:28 am Post subject: Re: templates for compile time switches |
|
|
| Quote: | I am tryng to figure out if I could reduce "#ifdef
...#else" clutter in the source code. (I have dozens of
such symbols defined from various sources and hence the
query)
Here is an example:
--------------------------------
#define DEBUG_PRINT
#ifdef DEBUG_PRINT
const bool bDebugPrint = true;
#else
const bool bDebugPrint = false;
#endif
|
If you want bDebugPrint to be totally optimized out (i.e.
so that there is no memory allocated for it), add "static".
| Quote: | template<bool T_cond> class CCondExec {
public:
CCondExec(void (*f)(void)) {}
inline void operator()() {}
}; // template<bool T_cond> class CCondExec {
template<> class CCondExec<true> {
void (*m_func)(void);
public:
CCondExec(void (*f)(void)) { m_func = f; }
inline void operator()() { m_func(); }
}; // template<bool T_cond> class CCondExec {
void debugPrint() {
cout << "debugPrint() called" << endl;
}
int main(int argc, char *argv[])
{
static CCondExec
condExec();
}
----------------------
This calls debugPrint() only if DEBUG_PRINT is defined.
Otherwise the call is a no-op.
My query is concerning efficiency of this approach.
|
You have one call via function pointer with debugging on,
but in a moment you are using I/O, so this little overhead
does not matter. That would be different if you would, say,
be logging to a memory buffer instead.
| Quote: | Is there a better way?
|
You do not state your goals. Is your goal to be able to
write one short call instead of
#ifdef DEBUG_PRINT
debugPrint();
#endif
?
Do you have one 'debugPrint()' or are there several such
functions?
Anyway, templates seem to be an overkill here. These are
two lighter approaches:
(1) At the beginning
#ifndef DEBUG_PRINT
# error DEBUG_PRINT must be defined
# error and convertible to bool
#endif
static const bool
must_be_convertible_to_bool = DEBUG_PRINT;
Further:
DEBUG_PRINT ? doSomething(),0:0;
or with blocks:
if (DEBUG_PRINT) {
....
....
}
(2) At the beginning
#ifdef DEBUG_PRINT
# define DEBUG_EXEC(cmd) cmd
#else
# define DEBUG_EXEC(cmd)
#endif
Further:
DEBUG_EXEC(doSomething());
HTH
Grzegorz
--
Free C++ frontend library: http://opencxx.sourceforge.net
China from the inside: http://www.staryhutong.com
Myself: http://www.dziupla.net/gj/cv
[ 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: Thu Nov 18, 2004 12:20 am Post subject: Re: templates for compile time switches |
|
|
"jakacki" <jakacki_hates_spam (AT) acm (DOT) org> wrote
| Quote: | I am tryng to figure out if I could reduce "#ifdef > ...#else"
clutter in the source code. (I have dozens of > such symbols defined
from various sources and hence the > query)
Here is an example:
--------------------------------
#define DEBUG_PRINT
#ifdef DEBUG_PRINT
const bool bDebugPrint = true;
#else
const bool bDebugPrint = false;
#endif
If you want bDebugPrint to be totally optimized out (i.e. so that
there is no memory allocated for it), add "static".
|
Adding static changes absolutely nothing here as far as the compiler is
concerned, since const variables at namespace scope are static by
default.
For the reader, of course, it is preferable to use the static.
--
James Kanze GABI Software http://www.gabi-soft.fr
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 |
|
 |
James Hopkin Guest
|
Posted: Fri Nov 19, 2004 12:58 pm Post subject: Re: templates for compile time switches |
|
|
[email]kanze (AT) gabi-soft (DOT) fr[/email] wrote:
| Quote: |
For the reader, of course, it is preferable to use the static.
|
Isn't static deprecated in this context? Putting the constant in a
nameless namespace would be correct.
James
[ 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
|
|