473,657 Members | 2,432 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1722
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*******@gmai l.comwrote in message
news:11******** *************@m 37g2000prh.goog legroups.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" <johnqREMOVETHI Sprogram...@yah oo.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.l earn.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...@spam cop.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" <johnqREMOVETHI Sprogram...@yah oo.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*********@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
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...@spam cop.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 objektorientier ter Datenverarbeitu ng
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
10151
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 online at http://www.parashift.com/c++-faq-lite/containers-and-templates.html#faq-34.15 Below are the two files that I compile with g++ foo.cpp -o foo or
3
1606
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, operator= gets called twice and I'm not sure whay that is. This fails on Solaris with: In function 'int main(int, char**)' error: no match for 'operator=' in 't2 = TestCL::getTestCL()()'
19
2789
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 version of that type. Is this a bug that is specific to gcc, or is it a flaw in the language specification that gcc diligently implements? For example, the below program produces the output Constant Mutable
7
1795
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 disappears. #include "stdio.h" int main() { char * const str="ya";
204
12984
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 = {0,1,2,4,9};
6
4133
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
15671
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; some may find it off-topic or confusing. Disclaimers at end. The code samples are intended to be nearly minimal demonstrations. They are *not* related to any actual application code.
3
4325
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
2903
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 floating point and integer intensive The gcc optimized (-O3) vs non optimized improved the performance 30 %
27
3058
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 the source (gcc-4.3.1/gcc/c-common.c) of gcc 4.3.1, is a syntax error. I'm inclined to agree, as it is like no C I have ever met. what is "C_COMMON_FIXED_TYPES (, fract);" supposed to mean? Could it be
0
8413
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8324
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8513
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6176
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5642
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2742
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1733
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.