Bug 51438 - std::exception and derived classes are not compatible with std::nested_exception and C++11 in general
Summary: std::exception and derived classes are not compatible with std::nested_except...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 4.7.0
: P3 normal
Target Milestone: 4.7.0
Assignee: Paolo Carlini
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2011-12-06 16:58 UTC by Sergey Zubkov
Modified: 2011-12-06 20:07 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2011-12-06 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Sergey Zubkov 2011-12-06 16:58:11 UTC
As implemented in gcc-4.7.0_alpha20111126, struct _Nested_exception has an implicit destructor, while std::exception and all <stdexcept> exception classes have explicit destructors with "throw()".

Because of this, it's impossible to use std::throw_with_nested with any exception derived from std::exception:

#include <exception>
#include <stdexcept>
int main()
{
    try {
        throw 2;
    } catch(int) {
        std::throw_with_nested(std::runtime_error("test"));
    }
}

gives compilation error:

In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.0-alpha20111126/include/g++-v4/exception:155:0,
                 from test.cc:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.0-alpha20111126/include/g++-v4/bits/nested_exception.h: In instantiation of ‘struct std::_Nested_exception<std::runtime_error>’:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.0-alpha20111126/include/g++-v4/bits/nested_exception.h:126:60:   required from ‘void std::__throw_with_nested(_Ex&&, ...) [with _Ex = std::runtime_error]’
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.0-alpha20111126/include/g++-v4/bits/nested_exception.h:140:7:   required from ‘void std::throw_with_nested(_Ex) [with _Ex = std::runtime_error]’
test.cc:8:58:   required from here
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.0-alpha20111126/include/g++-v4/bits/nested_exception.h:78:12: error: looser throw specifier for ‘virtual std::_Nested_exception<std::runtime_error>::~_Nested_exception()’
In file included from test.cc:2:0:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.0-alpha20111126/include/g++-v4/stdexcept:122:13: error:   overriding ‘virtual std::runtime_error::~runtime_error() throw ()’



suggested fix: remove "throw()" in <exception> and <stdexcept> in C++11 mode.
The C++11 version of class exception should have "virtual ~exception();" and other member functions should be noexcept (per §18.8.1) and the <stdexcept> classes should have implicit dtors (per §19.2).
Comment 1 Jonathan Wakely 2011-12-06 17:10:46 UTC
Or we could put noexcept on ~nested_exception()
Comment 2 Paolo Carlini 2011-12-06 17:19:54 UTC
For now, let's minimally use _GLIBCXX_NOEXCEPT everywhere on those destructors. By the way, in case isn't clear to everybody, most (all?) of such annoyances are ultimately due to PR50043.
Comment 3 Paolo Carlini 2011-12-06 17:48:25 UTC
I meant _GLIBCXX_USE_NOEXCEPT of course.
Comment 4 paolo@gcc.gnu.org 2011-12-06 20:03:29 UTC
Author: paolo
Date: Tue Dec  6 20:03:25 2011
New Revision: 182064

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=182064
Log:
2011-12-06  Paolo Carlini  <paolo.carlini@oracle.com>

	PR libstdc++/51438
	* libsupc++/nested_exception.h (nested_exception::~nested_exception):
	Declare noexcept.
	* libsupc++/nested_exception.cc: Adjust.
	* testsuite/18_support/nested_exception/51438.cc: New.
	* testsuite/18_support/nested_exception/throw_with_nested.cc: Adjust.
	* testsuite/18_support/nested_exception/rethrow_if_nested.cc:
	Likewise.

	* src/shared_ptr.cc: Use noexcept where appropriate.
	* include/std/system_error: Likewise.
	* include/std/functional: Likewise.
	* include/bits/shared_ptr_base.h: Likewise.
	* src/stdexcept.cc: Use _GLIBCXX_USE_NOEXCEPT where appropriate.
	* include/std/stdexcept: Likewise.
	* libsupc++/bad_cast.cc: Likewise.
	* libsupc++/bad_typeid.cc: Likewise.
	* libsupc++/eh_exception.cc: Likewise.
	* libsupc++/typeinfo: Likewise.
	* libsupc++/exception: Likewise.
	* libsupc++/eh_ptr.cc: Likewise.
	* libsupc++/bad_alloc.cc: Likewise.
	* libsupc++/exception_ptr.h: Likewise.

	* include/std/chrono: Use noexcept where appropriate.
	* src/chrono.cc: Likewise.

Added:
    trunk/libstdc++-v3/testsuite/18_support/nested_exception/51438.cc
Modified:
    trunk/libstdc++-v3/ChangeLog
    trunk/libstdc++-v3/include/bits/shared_ptr_base.h
    trunk/libstdc++-v3/include/std/chrono
    trunk/libstdc++-v3/include/std/functional
    trunk/libstdc++-v3/include/std/stdexcept
    trunk/libstdc++-v3/include/std/system_error
    trunk/libstdc++-v3/libsupc++/bad_alloc.cc
    trunk/libstdc++-v3/libsupc++/bad_cast.cc
    trunk/libstdc++-v3/libsupc++/bad_typeid.cc
    trunk/libstdc++-v3/libsupc++/eh_exception.cc
    trunk/libstdc++-v3/libsupc++/eh_ptr.cc
    trunk/libstdc++-v3/libsupc++/exception
    trunk/libstdc++-v3/libsupc++/exception_ptr.h
    trunk/libstdc++-v3/libsupc++/nested_exception.cc
    trunk/libstdc++-v3/libsupc++/nested_exception.h
    trunk/libstdc++-v3/libsupc++/typeinfo
    trunk/libstdc++-v3/src/chrono.cc
    trunk/libstdc++-v3/src/shared_ptr.cc
    trunk/libstdc++-v3/src/stdexcept.cc
    trunk/libstdc++-v3/testsuite/18_support/nested_exception/rethrow_if_nested.cc
    trunk/libstdc++-v3/testsuite/18_support/nested_exception/throw_with_nested.cc
Comment 5 Paolo Carlini 2011-12-06 20:07:59 UTC
Done.