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

Precompiler question


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
Jul 22 '05 #1
7 1370
Martin Stich wrote:

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?

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #2

"Martin Stich" <ms****@primusnetz.de> wrote in message
news:bq*************@news.t-online.com...

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
Jul 22 '05 #3

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
Jul 22 '05 #4
Chris Theis wrote:

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.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #5

"Pete Becker" <pe********@acm.org> wrote in message
news:3F***************@acm.org...
Martin Stich wrote:

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.


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
Jul 22 '05 #6
Paul Mensonides wrote:

"Pete Becker" <pe********@acm.org> wrote in message
news:3F***************@acm.org...
Martin Stich wrote:

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.


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.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #7

"Pete Becker" <pe********@acm.org> wrote in message
news:3F***************@acm.org...
Chris Theis wrote:

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
Jul 22 '05 #8

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

Similar topics

2
by: Carlos De Matos | last post by:
Hello all, Has anyone written a project/code/document on a possible implementation for a c# precompiler? I want to build a precompiler that will modify attributed code, then submit the...
8
by: Bill Pursell | last post by:
This question must be FAQ, but I can't find it: I'm baffled by the following: $ cat a.c #define foo(x) foo##x_ #define bar(x) bar##x##_ foo() foo(1) bar()
10
by: RainerF | last post by:
Hi Folks, My Problem is: My SQL Statement Structure: SELECT TABLEA.FIELDA, TABLEA.FIELDB, TABLEA.FIELDC, TABLEA.FIELDD,
22
by: JohnQ | last post by:
Is the C++ precompiler the same as a C precompiler? If not, how much different is it? John
104
by: JohnQ | last post by:
Well apparently not since one can step thru template code with a debugger. But if I was willing to make the concession on debugging, templates would be strictly a precompiler thing? I have a...
5
by: arjor | last post by:
hey guys i was wondering what's the difference between the following commands: #ifndef #if !defined looks to be the same or is it ?
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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
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...

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.