Precompiler question

M

Martin Stich

hi,
i would like to #define my own cp_new macro like this:
#define cp_new new(__FILE__,__LINE__)
for my custom new operator. this works fine so far.

however, i would additionally like to disallow the use of
the original new (i dont want to overload the global op. new
though). i thought of something like this:

#define new {DONT_USE_NEW_ANY_MORE}

so that it wont even compile. but then, the macro in the first
place wont compile either, of course. is there a way to
accomplish this ?

thanks
martin
 
P

Pete Becker

Martin said:
i thought of something like this:

#define new {DONT_USE_NEW_ANY_MORE}

so that it wont even compile. but then, the macro in the first
place wont compile either, of course.

It should compile just fine.
is there a way to
accomplish this ?

Don't do it. Redefining keywords is a bad idea. For example, what will
you do with a third-party library whose headers use new?
 
C

Chris Theis

Martin Stich said:
hi,
i would like to #define my own cp_new macro like this:
#define cp_new new(__FILE__,__LINE__)
for my custom new operator. this works fine so far.

however, i would additionally like to disallow the use of
the original new (i dont want to overload the global op. new
though). i thought of something like this:

#define new {DONT_USE_NEW_ANY_MORE}

This should be okay, but why don't you call your macro new which will solve
the problems of other people's code using new instead of cp_new? I'd suggest
to use #define new new(__FILE__, __LINE__) and implement you specialized
behavior in an overloaded version of the global new operator. This word just
fine for me implementing a memory leak detection mechanism. However, one has
to say the redefinition of keywords is in general not a good idea because it
might introduce very unwanted and weird behavior which is hard to trace. If
I remember correctly there was an article by Herb Sutter about that in C++
Users Journal some time ago.
so that it wont even compile. but then, the macro in the first
place wont compile either, of course. is there a way to
accomplish this ?

thanks
martin

Regards
Chris
 
M

Martin Stich

This should be okay, but why don't you call your macro new which will solve
the problems of other people's code using new instead of cp_new? I'd
suggest

because i'd also like to be able to circumvent the leak detection in very
special
cases, e.g. by something like raw_new which should do just what the original
new does. but this would then compile to new(__FILE__,__LINE__) which
is not what i want ;)
hm maybe i should write another version of new, which has an additional
parameter
for circumventing the leak detection...

thanks
martin
 
P

Pete Becker

Chris said:
This should be okay, but why don't you call your macro new which will solve
the problems of other people's code using new instead of cp_new?

It breaks other people's code that uses placement new:

char data[28];
new(data)B;

Don't do it.
 
P

Paul Mensonides

Pete Becker said:
It should compile just fine.

Out of curiosity, why are 'new' and 'delete' in the preprocessing-op-or-punc
production? This kind of implies that they cannot be identifiers even to the
preprocessor though the actual text of the standard makes no mention of that
being the case (though it does for the alternative tokens).

Regards,
Paul Mensonides
 
P

Pete Becker

Paul said:
Out of curiosity, why are 'new' and 'delete' in the preprocessing-op-or-punc
production? This kind of implies that they cannot be identifiers even to the
preprocessor though the actual text of the standard makes no mention of that
being the case (though it does for the alternative tokens).

This is far outside my area of expertise, but it looks to me like 'new'
and 'delete' are valid identifiers in phase 4, which is when
preprocessor directives are processed. It's not until phase 7 that
they're treated as preprocessing-op-or-punc, and get converted to
tokens.
 
C

Chris Theis

Pete Becker said:
Chris said:
This should be okay, but why don't you call your macro new which will solve
the problems of other people's code using new instead of cp_new?

It breaks other people's code that uses placement new:

char data[28];
new(data)B;

Which is of course true. Although for my special case (a quick leak
detection) it is okay. If you've got a more sophisticated approach I'd be
grateful if you could tell me about it 'cause I'm totally aware of the
aforementioned shortcoming.
Don't do it.

In general I totally agree with you.

Regards
Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top