473,503 Members | 3,497 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Valid code or not?

WDS
I was compiling some old code with a new compiler and it flagged a
statement as an error. This code compiled (and ran) fine with an
older version of the same compiler. The following snippet
demonstrates:

#include <vector>

void P()
{
std::vector<boolp;
p.std::~vector<bool>(); // Line 6
}

It that actually valid code? I tried the snippet with various
compilers and here's what I found:

#1: gCC

gCC 3.3.2 accepts it but 3.4.5 complains:

t.C: In function `void P()':
t.C:6: error: `std::~std::vector<bool, std::allocator<bool' is not
a member of `std::vector<bool, std::allocator<bool'

#2: xlC

AIX xlC older versions accept it but the latest version complains:

"t.C", line 6.24: CZP0157(30) The text ">" is unexpected. It may be
that this token was intended as a template argument list terminator
but the name is not known to be a template.

#3: Visual C++

Both Microsoft Visual 2005 and 2008 C++ complain:

t.cpp(6) : error C2588: '::~vector' : illegal global destructor
Jun 27 '08 #1
5 2582
WDS wrote:
I was compiling some old code with a new compiler and it flagged a
statement as an error. This code compiled (and ran) fine with an
older version of the same compiler. The following snippet
demonstrates:

#include <vector>

void P()
{
std::vector<boolp;
p.std::~vector<bool>(); // Line 6
}

It that actually valid code? I tried the snippet with various
compilers and here's what I found:

#1: gCC

gCC 3.3.2 accepts it but 3.4.5 complains:

t.C: In function `void P()':
t.C:6: error: `std::~std::vector<bool, std::allocator<bool' is not
a member of `std::vector<bool, std::allocator<bool'

#2: xlC

AIX xlC older versions accept it but the latest version complains:

"t.C", line 6.24: CZP0157(30) The text ">" is unexpected. It may be
that this token was intended as a template argument list terminator
but the name is not known to be a template.

#3: Visual C++

Both Microsoft Visual 2005 and 2008 C++ complain:

t.cpp(6) : error C2588: '::~vector' : illegal global destructor
It's invalid.

F
Jun 27 '08 #2
WDS
On Jun 23, 10:24*am, Fei Liu <fei....@gmail.comwrote:
It's invalid.
OK, which compiler's explanation (if any) is correct as to why it is
invalid?
Jun 27 '08 #3
On Jun 23, 8:21*am, WDS <B...@seurer.netwrote:
void P()
{
* * std::vector<boolp;
* * p.std::~vector<bool>(); *// Line 6

}
Regardless of your compilation i think your destructor will get called
2 times and your code will be undefined.

http://www.parashift.com/c++-faq-lit....html#faq-11.5

By the way, do you really need the std qualification on Line 6. It
seems to compile on gcc 4.x if you remove the std qualification there.

Ivan Novick
http://www.mycppquiz.com/
Jun 27 '08 #4
WDS
On Jun 23, 12:54*pm, Ivan Novick <i...@novickmail.comwrote:
On Jun 23, 8:21*am, WDS <B...@seurer.netwrote:void P()
{
* * std::vector<boolp;
* * p.std::~vector<bool>(); *// Line 6
}

Regardless of your compilation i think your destructor will get called
2 times and your code will be undefined.
Actually, the original code has pointers, kind of like this:

std::vector<bool*p;
p->std::~vector<bool>(); // Line 6

I was playing around with it and had switched to the non-pointer
version just before posting.
http://www.parashift.com/c++-faq-lit....html#faq-11.5

By the way, do you really need the std qualification on Line 6. *It
seems to compile on gcc 4.x if you remove the std qualification there.
Dunno. It was there and worked OK before I switched compilers.
Jun 27 '08 #5
On Jun 23, 5:21 pm, WDS <B...@seurer.netwrote:
I was compiling some old code with a new compiler and it
flagged a statement as an error. This code compiled (and ran)
fine with an older version of the same compiler. The
following snippet demonstrates:
#include <vector>
void P()
{
std::vector<boolp;
p.std::~vector<bool>(); // Line 6
}
It that actually valid code?
I can't tell, reading the standard. What's interesting is that
given:

namespace N {
enum E { a, b, c } ;
class C {} ;
}

int
main()
{
N::E e ;
N::C c ;
e.N::~E() ;
c.N::~C() ;
}

g++ (4.1.0) accepts the first destructor call, but not the
second. In your example, both
p.std::vector<bool>::~vector<bool>() and p.~vector<bool>() work,
but qualifying with the namespace, but not the class name,
doesn't.

This is covered in several different places in the standard, and
it seems possible as irrational as it seems, this behavior from
g++ is what the standard requires. The standard seems to
distinguish between "pseudo destructor calls" (§5.2.4, for
non-class types) and "explicit destructor calls" (§12.4/11, for
class types). I can't find any grammar productions for the
latter, however. All of the examples under consideration
correspond to the grammar for a pseudo destructor call, but
§5.2.4 seem to imply that there is a semantic restriction on
this production, and that it can only be used if the type name
is not a class type. I would have imagined that the intent was
(or should have been) that the grammar for both explicit
destructor calls and pseudo destructor calls be the same, and
that only the semantics depend on whether the object is a class
type or not. (Another interesting question is if the type name
is an array of class types---an array is not a class type, even
if its elements are, and §5.2.4 would seem to say that the
following is legal:

class C { public : ~C() ; } ;
typedef C A[5] ;

int main()
{
A a ;
a.~A() ;
}

but that the "pseudo destructor call" doesn't actually call any
destructors.)
I tried the snippet with various compilers and here's what I
found:
#1: gCC
gCC 3.3.2 accepts it but 3.4.5 complains:
t.C: In function `void P()':
t.C:6: error: `std::~std::vector<bool, std::allocator<bool' is not
a member of `std::vector<bool, std::allocator<bool'
#2: xlC
AIX xlC older versions accept it but the latest version complains:
"t.C", line 6.24: CZP0157(30) The text ">" is unexpected. It
may be that this token was intended as a template argument
list terminator but the name is not known to be a template.
#3: Visual C++
Both Microsoft Visual 2005 and 2008 C++ complain:
t.cpp(6) : error C2588: '::~vector' : illegal global destructor
Given the ambiguities in the standard, I'm not too surprised
that compilers differ, although from what you say, the evolution
seems to be in the same direction. You can always drop all
namespace qualifications for a class type; the fact that the
type to the left of the . or the -is a class type conditions
name lookup in such a way that the code will work. If the code
is in a template, however, and you don't know whether the type
is a class type or not, you may have a problem. (On the other
hand, I can't think of a case where you wouldn't know except
when the type is an instantiation type of the template. And you
don't want a namespace qualifier there.)

--
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
Jun 27 '08 #6

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

Similar topics

12
8143
by: lawrence | last post by:
I have a string which I want to send to eval(). How can I test it ahead of time to make sure it is valid code? I don't want to send it to eval and get parse errors. I want to do something like...
8
5721
by: Hostile17 | last post by:
Consider the following HTML. ---------- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd"> <html> <head> <meta...
0
3609
by: Tao | last post by:
I just upgraded .NET framework to 1.1 and VS.Net to 2003 version and tried to test it out. I created an ASP.NET project using the wizard and tried to run it by hitting "F5". I got an exception:...
1
2134
by: jel | last post by:
I ran what was submitted in several forums, but it's not exactly what i'm looking for. I'm dy'n over here. Ah, the frustrations of an amateur programmer. I included the code below in c++. which...
7
7268
by: JR | last post by:
Hey all, I have read part seven of the FAQ and searched for an answer but can not seem to find one. I am trying to do the all too common verify the data type with CIN. The code from the FAQ...
64
3435
by: jrefactors | last post by:
Since C is a subset of C++, so any C code or C libraries (printf(), scanf(), etc...) are valid C++ code. Is that correct? so even though the whole program is written in C, but with .cpp...
23
1883
by: James Aguilar | last post by:
Someone showed me something today that I didn't understand. This doesn't seem like it should be valid C++. Specifically, I don't understand how the commas are accepted after the function...
3
14510
by: Chris | last post by:
Hi, In C# I tried to save a file from a generated file name. Just before launching the dialog I check for a valid file name to be sure. There for I used the method ValidateNames from the save...
8
4829
by: Pieter | last post by:
Hi, I'm having some weird problem using the BackGroundWorker in an Outlook (2003) Add-In, with VB.NET 2005: I'm using the BackGroundWorker to get the info of some mailitems, and after each item...
10
16377
by: dba123 | last post by:
Why am I getting this error for Budget? Error: An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code Additional information: String was not...
0
7064
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
7261
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
7315
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6974
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...
1
4991
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...
0
4665
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...
0
1492
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 ...
1
721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
369
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...

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.