C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Throwing destructor and Standard Container

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
mdlinux7@yahoo.co.in
Guest





PostPosted: Sat Apr 23, 2005 2:19 am    Post subject: Throwing destructor and Standard Container Reply with quote



Most of the sites have quoted :
Objects with throwing destructors cannot be used in Standard
containers

The following code complies with gcc compiler [3.2.2-5] as i was
expecting the compiler error on the basis of above statement:

#include <iostream>
#include <vector>

using std::vector;
using std::cout;
using std::endl;

class bar
{
public:
bar()
{ cout<<"::Constructor of Bar Called::"< }
~bar()
{
throw 1;
cout<<"::Destructor Called for Bar class::"< }
};

typedef vector
main()
{
bar objBar;
barVector bVector;
bVector.push_back(objBar);
}

Could any one guide me that why the above code compiles ?

Regards
Dinesh


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Alf P. Steinbach
Guest





PostPosted: Sat Apr 23, 2005 5:16 pm    Post subject: Re: Throwing destructor and Standard Container Reply with quote



* [email]mdlinux7 (AT) yahoo (DOT) co.in[/email]:
Quote:
Most of the sites have quoted :
Objects with throwing destructors cannot be used in Standard
containers

The following code complies with gcc compiler [3.2.2-5] as i was
expecting the compiler error on the basis of above statement:

Why did you exepect a compiler error for a run-time issue?


Quote:
#include <iostream
#include
using std::vector;
using std::cout;
using std::endl;

class bar
{
public:
bar()
{ cout<<"::Constructor of Bar Called::"< }
~bar()
{
throw 1;
cout<<"::Destructor Called for Bar class::"< }
};

typedef vector
main()

'main' must have result type 'int'.


Quote:
{
bar objBar;
barVector bVector;
bVector.push_back(objBar);
}

Could any one guide me that why the above code compiles ?

It shouldn't compile due to missing result type for 'main'; you have a less
than perfect compiler.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
ben
Guest





PostPosted: Sun Apr 24, 2005 9:14 am    Post subject: Re: Throwing destructor and Standard Container Reply with quote



Because the destructor is not DECLARED as one that will throw an exception,
try:

~bar() throw (std::exception);

<mdlinux7 (AT) yahoo (DOT) co.in> wrote

Quote:
Most of the sites have quoted :
Objects with throwing destructors cannot be used in Standard
containers

The following code complies with gcc compiler [3.2.2-5] as i was
expecting the compiler error on the basis of above statement:

#include <iostream
#include
using std::vector;
using std::cout;
using std::endl;

class bar
{
public:
bar()
{ cout<<"::Constructor of Bar Called::"< }
~bar()
{
throw 1;
cout<<"::Destructor Called for Bar class::"< }
};

typedef vector
main()
{
bar objBar;
barVector bVector;
bVector.push_back(objBar);
}

Could any one guide me that why the above code compiles ?


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
James Kanze
Guest





PostPosted: Sun Apr 24, 2005 8:57 pm    Post subject: Re: Throwing destructor and Standard Container Reply with quote

[email]mdlinux7 (AT) yahoo (DOT) co.in[/email] wrote:
Quote:
Most of the sites have quoted :
Objects with throwing destructors cannot be used in Standard
containers

Which is correct according to the standard.

Quote:
The following code complies with gcc compiler [3.2.2-5] as i
was expecting the compiler error on the basis of above
statement:

The above statement says that you're not allowed to exit a
destructor via an exception if the object is in a standard
container. It doesn't say that you'll get an error message if
you do. In practice, it's pretty much impossible to know until
runtime anyway.

Quote:
#include <iostream
#include
using std::vector;
using std::cout;
using std::endl;

class bar
{
public:
bar()
{ cout<<"::Constructor of Bar Called::"< }
~bar()
{
throw 1;
cout<<"::Destructor Called for Bar class::"< }
};

typedef vector
main()
{
bar objBar;
barVector bVector;
bVector.push_back(objBar);
}

Could any one guide me that why the above code compiles ?

And where would you expect the error? Normally, the
implementation code for the destructor would be in a completely
different module; the compiler cannot see that it throws when it
instantiates the vector, and it cannot see that the object will
be used in a standard container when it compiles the destructor.

--
James Kanze mailto: [email]james.kanze (AT) free (DOT) fr[/email]
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 pl. Pierre Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Maxim Yegorushkin
Guest





PostPosted: Mon Apr 25, 2005 1:19 pm    Post subject: Re: Throwing destructor and Standard Container Reply with quote

On Sun, 24 Apr 2005 13:14:23 +0400, ben <benhongh (AT) hotmail (DOT) com> wrote:

Quote:
Because the destructor is not DECLARED as one that will throw an
exception,
try:

~bar() throw (std::exception);

It won't not help. The standard does not enforce the rule. Caveat emptor.

--
Maxim Yegorushkin

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.