C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

templates for compile time switches

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Srik
Guest





PostPosted: Wed Nov 10, 2004 4:25 pm    Post subject: templates for compile time switches Reply with 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

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





PostPosted: Tue Nov 16, 2004 11:28 am    Post subject: Re: templates for compile time switches Reply with quote



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





PostPosted: Thu Nov 18, 2004 12:20 am    Post subject: Re: templates for compile time switches Reply with quote



"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





PostPosted: Fri Nov 19, 2004 12:58 pm    Post subject: Re: templates for compile time switches Reply with quote

[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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.