473,414 Members | 1,698 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,414 software developers and data experts.

about marco in VC6

how can i let the follow marco works in VC6.0
#define TRACE_BEGIN(function) #ifdef DEBUG \

printf("----------------Enterinto%s-----------------\n",function);\
time_t beginTime,endTime;\
time(&beginTime); \
#endif
Jul 28 '05 #1
5 2934
* liwei:
how can i let the follow marco works in VC6.0
#define TRACE_BEGIN(function) #ifdef DEBUG \

printf("----------------Enterinto%s-----------------\n",function);\
time_t beginTime,endTime;\
time(&beginTime); \
#endif


First off, #define does not define things with preprocessor directive like
#ifdef in them. There's only one round of preprocessing of the text.

Second, DEBUG isn't a standard symbol, but NDEBUG (with opposite meaning)
is; NDEBUG is defined by <cassert>.

So, something like

#include <cassert>

#ifndef NDEBUG
# undef DEBUG
# define DEBUG
#endif

in a common miscellany header file, and then (including that header)

#ifndef DEBUG
# define TRACE_BEGIN( function )
#else
# define TRACE_BEGIN( function ) \
# printf ... \
#endif

However, the above is a C-style of doing things, and it's not only awkward
but can interact destructively with the client code.

In C++ you'd define a Tracer class and create a trace object, letting the
constrcutor do the TRACE_BEGIN things, and the destructor the TRACE_END
things. You might still use a macro to compile-time conditionally create a
Trace object or not. But again, that would be C-style; in C++ it would be
more natural to do the equivalent of

#ifdef DEBUG
# typedef Tracer int
#else
# typedef Tracer TracerImpl
#endif

To avoid all that preprocessing stuff everywhere you could use it once to
define a bool isDebugBuild constant, but explaining how to use that would go
beyond what's natural to discuss here.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 28 '05 #2
Alf P. Steinbach wrote:

#ifdef DEBUG
# typedef Tracer int
#else
# typedef Tracer TracerImpl
#endif


Because TracerImpl might (and probably does) have params to its
constructor, how about:

// PSEUDOCODE
template <bool b> class TracerImpl;
template<>
class TracerImpl<true> {
TracerImpl(sometype arg, etc...) // pseudocode
{
// do entry trace
}
~TracerImpl()
{
// do exit trace;
}
};
template<>
class TracerImpl<false> {
TracerImpl(sometype arg, etc...) { } // pseudocode
~TracerImpl() { }

};
#ifdef DEBUG
typedef TracerImpl<true> Tracer;
#else
typedef TracerImpl<false> Tracer;
#endif
Jul 28 '05 #3


red floyd schreef:
Alf P. Steinbach wrote:

#ifdef DEBUG
# typedef Tracer int
#else
# typedef Tracer TracerImpl
#endif


Because TracerImpl might (and probably does) have params to its
constructor, how about:

// PSEUDOCODE
template <bool b> class TracerImpl;
template<>
class TracerImpl<true> {
TracerImpl(sometype arg, etc...) // pseudocode
{
// do entry trace
}


That runs into the forwarding problem. However, if you replace
int by (void), it should work in all cases. The reason is that
(void)(arg1,arg2) is a cast expression, and , the comma operator.

HTH,
Michiel Salters

Jul 28 '05 #4
msalters wrote:

red floyd schreef:
Alf P. Steinbach wrote:

#ifdef DEBUG
# typedef Tracer int
#else
# typedef Tracer TracerImpl
#endif


Because TracerImpl might (and probably does) have params to its
constructor, how about:

// PSEUDOCODE
template <bool b> class TracerImpl;
template<>
class TracerImpl<true> {
TracerImpl(sometype arg, etc...) // pseudocode
{
// do entry trace
}

That runs into the forwarding problem. However, if you replace
int by (void), it should work in all cases. The reason is that
(void)(arg1,arg2) is a cast expression, and , the comma operator.

HTH,
Michiel Salters


What forwarding problem is that? Both template specializations are
fully defined, and we simply use the #ifdef/typedef combo to select
between them.

If you want to avoid templates:

class TracerImpl {
public:
TracerImpl(args) // PSEUDOCODE
{
#ifdef DEBUG
// tracing stuff here
#endif
}
~TracerImpl()
{
#ifdef DEBUG
// tracing stuff here
#endif
}
};
Jul 28 '05 #5
liwei wrote:
how can i let the follow marco works in VC6.0
#define TRACE_BEGIN(function) #ifdef DEBUG \

printf("----------------Enterinto%s-----------------\n",function);\
time_t beginTime,endTime;\
time(&beginTime); \
#endif
You can't use '#' expressions inside of a #define.
"It just doesn't work"(tm)

If you have a decent optimizer, you can use this version
and let the optimizer strip out the code when not needed.

#define TRACE_BEGIN(function) time_t beginTime,endTime;\
if (DEBUG) \
{printf("----------------Enterinto%s-----------------\n",function);\
time(&beginTime);}

It has the added benefit of checking your code for
syntax problems no matter how you compile it.

Or, you can swap the # directives around

#ifdef DEBUG
#define TRACE_BEGIN(function) \
printf("----------------Enterinto%s-----------------\n",function);\
time_t beginTime,endTime;\
time(&beginTime);
#else
#define TRACE_BEGIN(function)
#endif

If you are using am 'assert' package, you might want
to use NDEBUG instead of DEBUG, just to be consistant.

If you want to profile your code, doesn't VC have a code
profiling option? Most functions take so little time that
you will find that your profiling code is taking most of the
cpu-time, especially since printf is a slow bastard,
which skews your results. This is magnified if your
'TRACED' function calls other 'TRACED' functions.
You end up profiling your calls to printf, and optimizing
the wrong functions.

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Aug 7 '06 #6

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

Similar topics

39
by: Marco Aschwanden | last post by:
Hi I don't have to talk about the beauty of Python and its clear and readable syntax... but there are a few things that striked me while learning Python. I have collected those thoughts. I am...
3
by: Wei-Chao Hsu | last post by:
Hi, I read a Marco definition in a source file, which is shown below. #define BEGIN_EVENT_TABLE(theClass, baseClass) \ const wxEventTable theClass::sm_eventTable = \ {...
39
by: Kaarel | last post by:
I don't feel very confident when it comes to software licenses. But there are some cases I would like to make myself clear. What I am particulary interested in is when does GPL license become...
0
by: Marco | last post by:
Hello, I have to use the propertyReader of DSOFile.dll for reading/writing custom document properties in a closed document. In the msdn http://support.microsoft.com/?scid=kb;en-us;Q224351...
1
by: Ray | last post by:
Dear all, After the deployment of my vb.net program, I install it in my client's PC. After the deployment, the marco of Excel seems doesn't work. The combo box and the button in the excel cannot...
7
by: Plissken.s | last post by:
Hi, If I define a template wich uses the __FILE__ marco in a .h file and I have class which creates/uses that template (in a .cpp file). What will the __FILE__ marco expand to? the .h which...
2
by: m.arfini | last post by:
Hello, I must realize an application in c/c++ that allows me to open and to edit (in a basilar way) some video files (compressed and not). Searching on Internet I have found various libraries but I...
9
by: Marco Trapanese | last post by:
Hi, I need to insert some charts in my program. In detail, I acquire a lot of values from the serial ports and I want to plot them on a 2-axis chart. I know I can draw manually into a picture...
15
by: SM | last post by:
Hello, I have another simple question about an array in PHP and a variable in PHP. This is the array: $thumbs_cat_1 = array( 'wine', 'cheese', 'ice',
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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.