473,411 Members | 2,230 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,411 software developers and data experts.

GCC: non-conforming implementation of C++

GCC allows you to define macros called "new" and "delete" in the
preprocessor phase. This seems to be in violation of the C++ spec, in
particular the rules for the creation of preprocessing tokens.
Section 2.4 of ISO/IEC 14882:2003 states that preprocessing tokens
belong to one of the following groups

preprocessing-token:
header-name
identifier
pp-number
character-literal
string-literal
preprocessing-op-or-punc
each non-white-space character that cannot be one of the
above

Section 2.12 defines a preprocessing-op-or-punc to be:

preprocessing-op-or-punc: one of
{ } [ ] # ## ( )
<: : <% % %: %:%: ; : ...
new delete ? :: . .*
+ - * / % ˆ & | ̃
! = < += -= *= /= %=
ˆ= &= |= << > >>= <<= == !=
<= >= && || ++ -- , ->* ->
and and_eq bitand bitor compl not not_eq
or or_eq xor xor_eq

Since "new" and "delete" are preprocessing-op-or-punc's, does this not
violate the spec?

Regards
B.

Jul 27 '07 #1
9 1710
MS vc also allow new delete be defined as macro.

in fact, MFC take use of this.

2years ago, i use mfc in my project, and found
it generate many many compile error.
Finally, I found that MFC define keyword new at the
begin of the cpp file. and i #include STL headers
behind the that definition, so the compiler is angry.

I copy/paste the stl header include before the
define, all is ok.

That is my story, don't laugh at me...............

Jul 27 '07 #2

<bo*******@gmail.comwrote in message
news:11*********************@m37g2000prh.googlegro ups.com...

"GCC allows you to define macros called "new" and "delete" in the
preprocessor phase."

That's interesting. Can some please post those macro definitions here?

John

Jul 27 '07 #3
On Jul 28, 1:25 am, "JohnQ" <johnqREMOVETHISprogram...@yahoo.com>
wrote:
"GCC allows you to define macros called "new" and "delete" in the
preprocessor phase."

That's interesting. Can some please post those macro definitions here?

This code compiles in GCC.

#include <stdio.h>
#define new 1
#define delete 2
int main()
{

printf("%d\n", new + delete);
return 0;
}

While this is a simple example, new and delete can be defined to be
any macro.

Jul 27 '07 #4
On Fri, 27 Jul 2007 11:18:54 -0000, bo*******@gmail.com wrote in
comp.lang.c++:
GCC allows you to define macros called "new" and "delete" in the
preprocessor phase. This seems to be in violation of the C++ spec, in
particular the rules for the creation of preprocessing tokens.
Section 2.4 of ISO/IEC 14882:2003 states that preprocessing tokens
belong to one of the following groups
[snip]

Paragraph 2 of 17.4.3.1.1 Macro names forbids definition of keywords
as macros (note that new and delete are keywords). Attempting to do
so makes the program ill-formed and the behavior undefined. So the
compiler is free to do what it likes.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Jul 28 '07 #5
On Jul 28, 1:33 pm, Jack Klein <jackkl...@spamcop.netwrote:
>
Paragraph 2 of 17.4.3.1.1 Macro names forbids definition of keywords
as macros (note that new and delete are keywords). Attempting to do
so makes the program ill-formed and the behavior undefined. So the
compiler is free to do what it likes.
Hi Jack

Thanks for the reply. I am having difficulty understanding the
relevant portion of the spec you are referring to. I think this is
the portion that you draw the above from:

"Each name defined as a macro in a header is reserved to the
implementation for any use if the translation unit includes the
header.

A translation unit that includes a header shall not contain any macros
that define names declared or defined in that header. Nor shall such a
translation unit define macros for names lexically identical to
keywords."

Correct me if I'm wrong, but doesn't this only cover the case of a
source file which includes a header? What if the file does not
include a header?

Jul 28 '07 #6
Jack Klein wrote:
On Fri, 27 Jul 2007 11:18:54 -0000, bo*******@gmail.com wrote in
comp.lang.c++:
>GCC allows you to define macros called "new" and "delete" in the
preprocessor phase. This seems to be in violation of the C++ spec, in
particular the rules for the creation of preprocessing tokens.
Section 2.4 of ISO/IEC 14882:2003 states that preprocessing tokens
belong to one of the following groups

[snip]

Paragraph 2 of 17.4.3.1.1 Macro names forbids definition of keywords
as macros (note that new and delete are keywords). Attempting to do
so makes the program ill-formed
So far, I am with you.
and the behavior undefined. So the compiler is free to do what it likes.
That, I don't see. The paragraph reads:

A translation unit that includes a header shall not contain any macros
that define names declared or defined in that header. Nor shall such a
translation unit define macros for names lexically identical to keywords.

It looks like a diagnosable rule to me for which a diagnostic would be
required by [1.4/1]. After that diagnostic, the compiler is free to go
ahead an translate the code anyway.
Best

Kai-Uwe Bux
Jul 28 '07 #7
On Jul 28, 1:39 am, boroph...@gmail.com wrote:
On Jul 28, 1:25 am, "JohnQ" <johnqREMOVETHISprogram...@yahoo.com>
wrote:
"GCC allows you to define macros called "new" and "delete" in the
preprocessor phase."
That's interesting. Can some please post those macro definitions here?
This code compiles in GCC.
#include <stdio.h>
#define new 1
#define delete 2
int main()
{
printf("%d\n", new + delete);
return 0;
}
This happens to be a perfectly legal C program, so any C
compiler should compile it (and I doubt you'll find any that
don't). I imagine most C++ compilers will compile it as well,
simply because they tend to be compatible with C for this sort
of thing. Redefining a keyword by means of a macro is undefined
behavior if you include any C++ header, however (and <stdio.h>
is a C++ header, albeit a deprectated one, in a C++ program).
That doesn't mean it won't compile and run; it just means that
you have no idea what it might do.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
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

Jul 29 '07 #8
On Jul 27, 11:18 pm, boroph...@gmail.com wrote:
GCC allows you to define macros called "new" and "delete" in the
preprocessor phase. This seems to be in violation of the C++ spec
The spec only says that such a program isn't a
valid C++ program. It doesn't mandate that a
compiler can't compile it anyway.
Jul 30 '07 #9
On Jul 28, 5:33 am, Jack Klein <jackkl...@spamcop.netwrote:
On Fri, 27 Jul 2007 11:18:54 -0000, boroph...@gmail.com wrote in
comp.lang.c++:
GCC allows you to define macros called "new" and "delete" in the
preprocessor phase. This seems to be in violation of the C++ spec, in
particular the rules for the creation of preprocessing tokens.
Section 2.4 of ISO/IEC 14882:2003 states that preprocessing tokens
belong to one of the following groups
[snip]
Paragraph 2 of 17.4.3.1.1 Macro names forbids definition of keywords
as macros (note that new and delete are keywords). Attempting to do
so makes the program ill-formed and the behavior undefined. So the
compiler is free to do what it likes.
I think you're missing his point; I missed it too in an earlier
posting. The point is that according to §2.12, new and delete
are *not* keywords; they are preprocessing-op-or-punc. G++ does
complain if you try something like:
#define or &&
(or is also a preprocessing-op-or-punc).

Of course, the standard (apparently at least) contradicts itself
here, since in §2.11, it says that they are keywords (where as
or et al. explicitly aren't, but form another category:
alternate representations).

I rather think that it's an error that new and delete appear as
preprocessing-op-or-punc. They're listed as keywords as well
(and they aren't the only keywords which can be operators---the
new style casts involve a keyword as well, as does typeid).
I'll post a defect report in comp.std.c++.

--
James Kanze (GABI Software) email:ja*********@gmail.com
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

Jul 31 '07 #10

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

Similar topics

2
by: Christophe Barbe | last post by:
I posted a few days ago about the same problem but was not very clear. So here is my second take at it. Basically with GCC 3.3.2, I can't compile the example from the C++ FAQ Lite available...
3
by: jim.brown | last post by:
The attached code implements a test class to show an error in overloading operator=. This code works on Windows with Visual Studio and simpler cases work with gcc 3.3.2 on Solaris 9. On Windows,...
19
by: Christian Engström | last post by:
If you have a function that returns something by value, the gcc compiler (version 3.2.3 on Windows XP with MinGW) converts the returned value from the type you specify in the code, to the const...
7
by: Victor Irzak | last post by:
Hello! This program causes seg fault on gcc, but executes fine on icc and VC7. Is there a reason for it or is it a bug? Note: if "char * const str" is changed to "char * str", the gcc problem...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
6
by: steve yee | last post by:
#define M(i, j) ({ ++i; j; }) int main(int argc, _TCHAR* argv) { int i = 0; int j = M(i, 3); return 0; } the above code can compile with gcc, but can't compile with most other
68
by: James Dow Allen | last post by:
The gcc compiler treats malloc() specially! I have no particular question, but it might be fun to hear from anyone who knows about gcc's special behavior. Some may find this post interesting;...
3
by: =?ISO-8859-1?Q?Glenn_M=F8ller-Holst?= | last post by:
Hi! How do I get generated ARM assembler from this compiler: #arm-elf-gcc -dumpversion 4.2.0 #arm-elf-gcc -dumpmachine arm-elf kind regards,
37
by: kumarchi | last post by:
hello: I recently compiled a numerically intensive c project under cygwin gcc 3.4.4 and microsoft visual c. The platform is intel T2400 1.83 ghz dual core lap top. the numerical stuff is both...
27
by: Dave | last post by:
I'm having a hard time tying to build gcc 4.3.1 on Solaris using the GNU compilers. I then decided to try to use Sun's compiler. The Sun Studio 12 compiler reports the following code, which is in...
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?
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...
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.