Thread View: comp.std.c++
5 messages
5 total messages
Started by Alan Griffiths
Fri, 05 Apr 1996 02:58
Must exception classes have copy constructors?
Author: Alan Griffiths
Date: Fri, 05 Apr 1996 02:58
Date: Fri, 05 Apr 1996 02:58
52 lines
2655 bytes
2655 bytes
I've just tried porting some code to MSVC4 (it compiles and works with
SC7.1 and BC4.5).
In effect it appears that the Microsoft compiler _requires_ anything
thrown as an exception to have an accessible copy constructor. (I've
tended to make copy constructors private to ensure catch-by-reference
and aviod slicing bugs.)
Looking through the DWP I can't see any reason to require a copy
constructor, but there are references to copying temporaries.
Is MSVC4 correct?
code:
class MyBaseException {
public: MyBaseException() {}
virtual char* what() = 0;
private: MyBaseException(const MyBaseException&);
};
class MyException1 : public MyBaseException {
public: virtual char* what() { return "MyException1"; }
};
int main() {
try {
throw MyException1();
// error C2700: 'class MyException1' : cannot be thrown (use -W4 for more info)
// warning C4671: 'MyException1' : the copy constructor is inaccessible
} catch (MyBaseException& e) {
cout << "OK - got here (" << __LINE__ << ") : have a " << e.what()
<< endl;
} catch (...) {
cout << "ERROR - got here (" << __LINE__ << ")" << endl;
}
return 0;
}
--
Alan Griffiths | Also editor of: The ISDF Newsletter
Senior Systems Consultant, | (An Association of C and C++ Users publication)
CCN Group Limited. | (ISDF editor : isdf@octopull.demon.co.uk)
(agriffiths@ma.ccngroup.com) | (For ACCU see : http://bach.cis.temple.edu/accu)
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Re: Must exception classes have copy constructors?
Author: David Byrden
Date: Sat, 06 Apr 1996 19:33
Date: Sat, 06 Apr 1996 19:33
25 lines
1786 bytes
1786 bytes
Alan Griffiths <aGriffiths@ma.ccngroup.com> wrote:
>I've just tried porting some code to MSVC4 (it compiles and works with
>SC7.1 and BC4.5).
>
>In effect it appears that the Microsoft compiler _requires_ anything
>thrown as an exception to have an accessible copy constructor. (I've
>tended to make copy constructors private to ensure catch-by-reference
>and aviod slicing bugs.)
>
>Looking through the DWP I can't see any reason to require a copy
>constructor, but there are references to copying temporaries.
What DWP did you read? The January 96 version requires that the
"throw" statement have access to the copy constructor and destructor of a
thrown object.
David
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Re: Must exception classes have copy constructors?
Author: Jason D. Morris
Date: Sat, 06 Apr 1996 19:33
Date: Sat, 06 Apr 1996 19:33
62 lines
2783 bytes
2783 bytes
On 05 Apr 96 02:58:28 GMT, Alan Griffiths <aGriffiths@ma.ccngroup.com>
wrote:
>I've just tried porting some code to MSVC4 (it compiles and works with
>SC7.1 and BC4.5).
>
>In effect it appears that the Microsoft compiler _requires_ anything
>thrown as an exception to have an accessible copy constructor. (I've
>tended to make copy constructors private to ensure catch-by-reference
>and aviod slicing bugs.)
>
>Looking through the DWP I can't see any reason to require a copy
>constructor, but there are references to copying temporaries.
>
>Is MSVC4 correct?
>
>code:
>class MyBaseException {
>public: MyBaseException() {}
> virtual char* what() = 0;
>private: MyBaseException(const MyBaseException&);
>};
>
>class MyException1 : public MyBaseException {
>public: virtual char* what() { return "MyException1"; }
>};
>
>int main() {
> try {
> throw MyException1();
>// error C2700: 'class MyException1' : cannot be thrown (use -W4 for more info)
>
>// warning C4671: 'MyException1' : the copy constructor is inaccessible
> } catch (MyBaseException& e) {
> cout << "OK - got here (" << __LINE__ << ") : have a " << e.what()
> << endl;
> } catch (...) {
> cout << "ERROR - got here (" << __LINE__ << ")" << endl;
> }
> return 0;
>}
>
Here's what BC++ 5.0 generates...
Borland C++ 5.0 for Win32 Copyright (c) 1993, 1996 Borland
International
MS-EXCPT.CPP:
Error MS-EXCPT.CPP 15: Compiler could not generate copy constructor
for class 'MyException1' in function main()
*** 1 errors in Compile ***
It appears we have an agreement in interpretation of the DWP.
Jason
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Re: Must exception classes have copy constructors?
Author: Steve Clamage
Date: Sat, 06 Apr 1996 19:35
Date: Sat, 06 Apr 1996 19:35
30 lines
2111 bytes
2111 bytes
Alan Griffiths <aGriffiths@ma.ccngroup.com> writes:
>I've just tried porting some code to MSVC4 (it compiles and works with
>SC7.1 and BC4.5).
>In effect it appears that the Microsoft compiler _requires_ anything
>thrown as an exception to have an accessible copy constructor. (I've
>tended to make copy constructors private to ensure catch-by-reference
>and aviod slicing bugs.)
According to the draft standard, when you throw something, what is
thrown is actually a copy of that something. If the copy constructor
for that something is not accessible, the copy can't be made.
Under some circumstances the copy can be optimized away. The draft
standard was recently clarified to say that when logically a copy
is needed, the copy constructor must be accessible even if in a
particular case a compiler happens to optimize away the copy.
In your case, you will probably have to throw a pointer instead
of a value, if the type has a private copy constructor.
--
Steve Clamage, stephen.clamage@eng.sun.com
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Re: Must exception classes have copy constructors?
Author: Rob Stewart
Date: Mon, 08 Apr 1996 18:44
Date: Mon, 08 Apr 1996 18:44
39 lines
2461 bytes
2461 bytes
Steve Clamage wrote: > > Alan Griffiths <aGriffiths@ma.ccngroup.com> writes: > > >I've just tried porting some code to MSVC4 (it compiles and works with > >SC7.1 and BC4.5). > > >In effect it appears that the Microsoft compiler _requires_ anything > >thrown as an exception to have an accessible copy constructor. (I've > >tended to make copy constructors private to ensure catch-by-reference > >and aviod slicing bugs.) > > According to the draft standard, when you throw something, what is > thrown is actually a copy of that something. If the copy constructor > for that something is not accessible, the copy can't be made. > [snip] > In your case, you will probably have to throw a pointer instead > of a value, if the type has a private copy constructor. You'll have to implement an exception object management policy. (I know the exception is actually the pointer to some class, but I'm using "exception object" to mean the object pointed to by that exception.) You must allocate your exception objects on the heap, and delete them in the final catch block. That is, so long as you throw the pointer up the stack, as it were, you cannot delete what the pointer points to. Only in the catch block in which you no longer rethrow the exception can you delete the memory (exception object) it points to. -- Robert Stewart | My opinions are usually my own. Datalytics, Inc. | stew@datalytics.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 ] [ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ] [ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ] [ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads