Topic: exceptions & copy constructors


Author: "Sergey P. Derevyago" <non-existent@iobox.com>
Date: Tue, 26 Dec 2000 17:53:49 GMT
Raw View
Let's consider the following function:

 X f(X a)
 {
  X b;
  return b;
 }

Now I'll try to call it:

 void g()
 {
  X x;
  X y=f(x);
 }

The value of x (in g) and the value of b (in f) will be copied.

What the standard says about the point of call of the (implicit) copy
constructors? Is it guaranteed that this constructors will alway be called only
outside or only inside f?

Why is it important? Let's consider the modified f:

 X f(X a) throw()
 {
  X b;
  return b;
 }

What if the copying of a throws an exception? What if the copying of b throws
an exception? Does the standard unambiguously resolve this issue? For example,
12.2 - Temporary objects [class.temporary] says that some of the temporaries
(with the corresponding copy constructor calls) can be throwed out. Can it lead
to std::terminate() call on one implementation and to an exception in g on
another implementation?
--
         With all respect, Sergey.          http://cpp3.virtualave.net/
         mailto : ders at skeptik.net

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]





Author: "Andrei Iltchenko" <iltchenko@yahoo.com>
Date: Wed, 27 Dec 2000 00:01:15 GMT
Raw View
> Let's consider the following function:
>
>  X f(X a)
>  {
>   X b;
>   return b;
>  }
>
> Now I'll try to call it:
>
>  void g()
>  {
>   X x;
>   X y=f(x);
>  }
>
> The value of x (in g) and the value of b (in f) will be copied.
>
> What the standard says about the point of call of the (implicit) copy
> constructors? Is it guaranteed that this constructors will alway be called
only
> outside or only inside f?
The parameter a of the function f will be initialized with class X's copy
constructor from the argument x of the function call f(x). And the copy
constructor will be executed before the function f is entered, i.e. in the
context
of the calling function g (5.2.2 paragraph 4).

When b (in f) is copied with X's copy constructor the copying happens in the
context of the function called - namely f.

>
> Why is it important? Let's consider the modified f:
>
>  X f(X a) throw()
>  {
>   X b;
>   return b;
>  }
>
> What if the copying of a throws an exception? What if the copying of b
throws
> an exception? Does the standard unambiguously resolve this issue?
If initialization of the parameter a from the argument x throws an
exception,
the function f will not be entered and the stack unwinding will start, and
the
search for a handler will take place in the scope of the calling function g.

If the copying of b throws an exception, the stack will be unwound for f
followed by a call to std::unexpected, as f, according to the piece of code
above, has an empty exception specification.

Andrei Iltchenko
Brainbench MVP for C++
http://www.brainbench.com



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]