473,406 Members | 2,345 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Text removing using a macro in a namespace

Hi,

I like to know if this is a good way to remove 'line 18' from
compilation when DBG is not defined (I dislike to wrap 'line 18' using
#if/#endif pair) of if there are better ways (always using the
namespace).

Am I sure that the compiler generates non code for 'line 18'?

/////////////////////////
//#define DBG

namespace MY_NAMESPACE
{
#if defined(DBG)
void MY_DUMP(int)
{
}
#else // release case
int pippo;
#define MY_DUMP(a) pippo
#endif
}

int main()
{
MY_NAMESPACE::MY_DUMP(1); // line 18
return 0;
}
////////////////////////

Thanks a lot.
Marco.

--
For direct reply change underscore to dot
Jul 22 '05 #1
6 1862

"marco_segurini" <ma***********@virgilio.it> wrote in message
news:a3*************************@posting.google.co m...
Hi,

I like to know if this is a good way to remove 'line 18' from
compilation when DBG is not defined (I dislike to wrap 'line 18' using
#if/#endif pair) of if there are better ways (always using the
namespace).

Am I sure that the compiler generates non code for 'line 18'?

/////////////////////////
//#define DBG

namespace MY_NAMESPACE
{
#if defined(DBG)
void MY_DUMP(int)
{
}
#else // release case
int pippo;
#define MY_DUMP(a) pippo
#endif
}

int main()
{
MY_NAMESPACE::MY_DUMP(1); // line 18
return 0;
}

Macros don't know anything about namespaces. Take a look at the preprocessed
output.

If DBG is not defined, you get:

namespace MY_NAMESPACE
{
int pippo;
}

int main()
{
MY_NAMESPACE::pippo;
return 0;
}

Otherwise, you get:

namespace MY_NAMESPACE
{
void MY_DUMP(int)
{
}
}

int main()
{
MY_NAMESPACE::MY_DUMP(1);
return 0;
}

It's hard for me to see why you would want either. What is your goal?

Jonathan
Jul 22 '05 #2
"marco_segurini" <ma***********@virgilio.it> wrote in message news:a3*************************@posting.google.co m...
Hi,

I like to know if this is a good way to remove 'line 18' from
compilation when DBG is not defined (I dislike to wrap 'line 18' using
#if/#endif pair) of if there are better ways (always using the
namespace).

Am I sure that the compiler generates non code for 'line 18'?
Most compilers would not. But there is a much clearer way
to say "do this" for one build and "do nothing" for another.
See insertions below. (I assume 'pippo' had no real purpose.)
/////////////////////////
//#define DBG

namespace MY_NAMESPACE
{
#if defined(DBG)
void MY_DUMP(int)
{
}
#else // release case #if 0 int pippo;
#define MY_DUMP(a) pippo #else
#define MY_DUMP(a)
#endif #endif
}

int main()
{
MY_NAMESPACE::MY_DUMP(1); // line 18
return 0;
}
////////////////////////

Thanks a lot. Good luck.
Marco.


--
--Larry Brasfield
email: do***********************@hotmail.com
Above views may belong only to me.
Jul 22 '05 #3
Thanks Jonathan Turkanis,
It's hard for me to see why you would want either. What is your goal?


My goal is to call a free-function defined in a namespace ONLY if DBG
is defined. I don't want to wrap all the free-function calls with {
#if DBG, #endif } pair.

Marco.
Jul 22 '05 #4
Thanks Larry Brasfield,

If I apply your modification with DBG non defined the preprocessor output is:

namespace MY_NAMESPACE
{
}

int main()
{
MY_NAMESPACE::; <<<<<<<<<<<<
return 0;
}

that is the source of all my problems.

Marco.
Jul 22 '05 #5

"marco_segurini" <ma***********@virgilio.it> wrote in message
news:a3*************************@posting.google.co m...
Thanks Jonathan Turkanis,
It's hard for me to see why you would want either. What is your goal?
My goal is to call a free-function defined in a namespace ONLY if DBG
is defined.


I see.
I don't want to wrap all the free-function calls with {
#if DBG, #endif } pair.
I don't blame you. Lot's of #ifdef's can be ugly.

Try:

#ifdef DBG
# define DEBUG_DUMP(x) my_namespace::debug_dump(x);
#else
# define DEBUG_DUMP(x)
#endif
Marco.


Jonathan
Jul 22 '05 #6
"marco_segurini" <ma***********@virgilio.it> wrote in message
news:a3*************************@posting.google.co m...
I like to know if this is a good way to remove 'line 18' from
compilation when DBG is not defined (I dislike to wrap 'line 18' using
#if/#endif pair) of if there are better ways (always using the
namespace).

Am I sure that the compiler generates non code for 'line 18'?


I personally would use the following (I modified your namespace to use mixed
case since you seem to use all upper for macros like many people):

namespace MyNamespace
{
inline void MyDump( int n )
{
#if defined DBG
// Code that handles dump
#endif
}
};

While it is true that you cannot know for certain that the compiler will
optimize this away, I would still say use this unless you know for certain
you cannot afford to. I write real-time embedded software and even I would
do it this way. Basically, my opinion is if the compiler doesn't perform
this trivial optimization then it didn't perform a million other
optimizations I wanted so that this one inefficiency will be insignificant.

If you insist on a macro I would suggest the following...it is a common
method used in assert macros. A constant will gobble up the semicolon as
well as a variable, and the void makes sure you can't do anything with it
(in other words, it simulates the void returned from the debug function so
they are usable in the same ways).

namespace MyNamespace
{
#if defined DBG
inline void MyDump( int n )
{
// Code that handles dump
}
#else
#define MyDump( n ) ( (void) 0 )
#endif
};

Donovan Hawkins
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Manoj G | last post by:
Hello, I believe there is no way to remove the default namespace declaration (For eg <DataSet xmlns="something">.... ) on an XmlNode object directly through DOM. So, what is the best way to...
3
by: Sin | last post by:
I'm currently evaluating VC.NET as the new platform for the company I work for and things are looking grim... We're up against another IDE which took me about 5 minutes to master and I've been...
3
by: Francesc | last post by:
Hi, I'm new at this newsgroup and I want do ask some questions and opinions about this subject. I'm developing an application focused in a very specific task: clean and labelling text documents...
6
by: Chris Chiasson | last post by:
Hi, After reading and experimenting for a several hours, I have this stylesheet: <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"...
3
by: casul | last post by:
Hi All, I was told there were a few macro gurus on this group :) I'm trying to define a macro that will allow me to write the following code : #include MY_MACRO( NAME, SPACE )
2
by: Daz | last post by:
Hi everyone. Is it possible to remove a script that has been added to a page? I have asked this question before, and was told that it was pretty much impossible to do, unless of course, I...
1
by: galewind | last post by:
Hi, I have exported a report to text file using the Outputto action in a macro. There is no problem in the format in the text file except that there is a blank line every 2/3 records. How to remove...
4
by: chimambo | last post by:
I have 2 problems: 1. I want to import a single text file into an access table using a Macro. I am however getting an error that I need to put a specification name argument. What does this mean?...
7
by: tshad | last post by:
I have a program in 2005 that is reading a text file removing text and then writing it back out again. It removes lines that start with PRINT. This program has worked fine for months. Now all...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.