473,652 Members | 3,049 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<boo lp;
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::vec tor<bool, std::allocator< bool' is not
a member of `std::vector<bo ol, 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 2592
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<boo lp;
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::vec tor<bool, std::allocator< bool' is not
a member of `std::vector<bo ol, 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.ne twrote:
void P()
{
* * std::vector<boo lp;
* * 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...@novickmai l.comwrote:
On Jun 23, 8:21*am, WDS <B...@seurer.ne twrote:void P()
{
* * std::vector<boo lp;
* * 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<boo l*p;
p->std::~vector<b ool>(); // 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.ne twrote:
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<boo lp;
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<b ool>::~vector<b ool>() 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::vec tor<bool, std::allocator< bool' is not
a member of `std::vector<bo ol, 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 objektorientier ter Datenverarbeitu ng
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
8172
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 this: $valid = checkPHP($string); if ($valid) { eval($string); } else { $resultsObject->addToErrorResults("We wanted to send our template to eval(), but the PHP it contained was invalid.");
8
5737
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 http-equiv=Content-Type content="text/html; charset=iso-8859-1"> <title>Untitled</title>
0
3625
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: "Specified cast is not valid." The only thing i put there was a "test this." inside the form. What might be the problem here? Thanks in advance. The Exception:
1
2146
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 details what i'm looking for. I'm sure its just a small detail. But I'm in the dark. // numbers1to10only.cpp #include <iostream.h>
7
7281
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 looks like this: #include <iostream>
64
3490
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 extension, we still consider as C++ program? Please advise. Thanks
23
1911
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 'fprintf'. What effect do they have in those parenthesis? I understand how the or is useful and why never to do it, I'm really just asking about that construction "(fprintf(stderr, "Can't open file.\n"), exit(0), 1))". ---- CODE ----
3
14526
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 dialog. The strange thing is that sometimes the save dialogue
8
4842
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 I want to raise the ProgressChanged-event to update the DataGridView. It works fine when only one Progresschanged is fired, but at the second, third, fopurth etc it raises everytile a 'Cross-thread operation not valid"-exception on lmy...
10
16405
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 recognized as a valid Boolean. Public Sub UpdateCustomer_DashboardGraphs(ByVal sender As Object, ByVal e As System.EventArgs)
0
8367
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
8279
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,...
0
8811
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7302
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6160
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
4145
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...
1
2703
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
1
1914
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1591
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.