PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > Throwing a reference to a temporary object
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Throwing a reference to a temporary object

Réponse
 
LinkBack Outils de la discussion
Vieux 17/10/2007, 10h25   #1 (permalink)
Dario Menendez
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Throwing a reference to a temporary object

Is it possible to throw a reference to a temporary object?
Will the temporary object be copied one or more times?

See following example:

struct my_exception
{
my_exception(int i) : i_(i) {}
my_exception& ref() {return *this;}
int i_;
}

void f_throws()
{
throw my_exception(3).ref();
}

int main()
{
try {
f_throws();
}
catch(my_exception& e) { return e.i_; }
return 0;
}

  Réponse avec citation
Vieux 17/10/2007, 10h53   #2 (permalink)
anon
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Throwing a reference to a temporary object

Dario Menendez wrote:
> Is it possible to throw a reference to a temporary object?
> Will the temporary object be copied one or more times?
>
> See following example:
>
> struct my_exception
> {
> my_exception(int i) : i_(i) {}
> my_exception& ref() {return *this;}
> int i_;
> }
>
> void f_throws()
> {
> throw my_exception(3).ref();
> }
>
> int main()
> {
> try {
> f_throws();
> }
> catch(my_exception& e) { return e.i_; }
> return 0;
> }
>


I modified your example bit:

#include <iostream>

struct my_exception
{
my_exception(int i) : i_(i)
{ std::cout<<"constr i="<<i<<std::endl;}
my_exception(const my_exception &c)
{ this->i_ = c.i_;std::cout<<"cc"<<std::endl;}
const my_exception& ref()
{ std::cout<<"in ref()"<<std::endl; return *this;}
int i_;
};

void f_throws()
{
std::cout<<"throwing"<<std::endl;
throw my_exception(3).ref();
//throw my_exception(3);
}

int main()
{
try
{
f_throws();
}
catch( const my_exception &e )
{
return e.i_;
}
return 0;
}

It gave me the answer to your question: one copy.
  Réponse avec citation
Vieux 17/10/2007, 11h06   #3 (permalink)
Neelesh Bodas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Throwing a reference to a temporary object

On Oct 17, 2:25 pm, Dario Menendez <dari...@gmail.com> wrote:
> Is it possible to throw a reference to a temporary object?
> Will the temporary object be copied one or more times?
>
> See following example:
>
> struct my_exception
> {
> my_exception(int i) : i_(i) {}
> my_exception& ref() {return *this;}
> int i_;
>
> }
>
> void f_throws()
> {
> throw my_exception(3).ref();
>
> }
>
> int main()
> {
> try {
> f_throws();
> }
> catch(my_exception& e) { return e.i_; }
> return 0;
>
> }





15.1(3) says : "A throw expression initializes a temporary object"
Thus, the object to be thrown is always copied, and it is always a
'copy' that is thrown, never the "actual" object associated with the
throw clause.

[as an aside: the standard allows o get rid of the temporary if doing
so doesnot alter the program semantics except for the constructor-
destructor execution]


try this out - make the teh copy constructor private. The code won't
compile anymore.

-N

  Réponse avec citation
Vieux 17/10/2007, 11h13   #4 (permalink)
Dario Menendez
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Throwing a reference to a temporary object

Thanks for the fast answer.

I was looking more for a theoretical guarantee from the language
specification about the copying of thrown objects.

Maybe compiler implementation details can affect this: optimization
could remove that copy or if the exception has to propagate through
more than one function call it could be copied more than once.

  Réponse avec citation
Vieux 17/10/2007, 11h18   #5 (permalink)
Dario Menendez
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Throwing a reference to a temporary object

Sorry, I posted before seeing reply from Neelesh Bodas.

I guess that answers my question. Thanks!

  Réponse avec citation
Vieux 18/10/2007, 08h26   #6 (permalink)
anon
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Throwing a reference to a temporary object

Dario Menendez wrote:
> Sorry, I posted before seeing reply from Neelesh Bodas.
>
> I guess that answers my question. Thanks!
>


Yes, his explanation is much better
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 01h53.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,09462 seconds with 14 queries