I'm not 100% this is valid, but it used to work before the changes for PR 68297: #include <exception> int i = 0; auto p = std::make_exception_ptr<int&>(i); It fails now because the new implementation uses the template argument as `_Ex*` and as `new _Ex` and those aren't valid for a reference type. And if we fix that, there's another problem which is that it uses typeid(__ex) which means we create an exception using the static type of the argument, but which matches a handler of the dynamic type: #include <exception> #include <stdlib.h> struct B { virtual ~B() = default; }; struct D : B { }; int main() { try { D d; auto p = std::make_exception_ptr<B&>(d); std::rethrow_exception(p); } catch (const D&) { abort(); } catch (const B&) { } } This throws a B but catches a D.
Also, since the changes for PR 90295 <bits/exception_ptr.h> is no longer usable in C++98: include/bits/exception_ptr.h:182: error: declaration of 'std::__exception_ptr::exception_ptr::exception_ptr()' has a different exception specifier include/bits/exception_ptr.h:107: note: from previous declaration 'std::__exception_ptr::exception_ptr::exception_ptr() throw ()' include/bits/exception_ptr.h:188: error: declaration of 'std::__exception_ptr::exception_ptr::exception_ptr(const std::__exception_ptr::exception_ptr&)' has a different exception specifier include/bits/exception_ptr.h:109: note: from previous declaration 'std::__exception_ptr::exception_ptr::exception_ptr(const std::__exception_ptr::exception_ptr&) throw ()'
The master branch has been updated by Jonathan Wakely <redi@gcc.gnu.org>: https://gcc.gnu.org/g:a1ca039fc0fe934ef36c25d8284e6e116bcaffa7 commit r12-5879-ga1ca039fc0fe934ef36c25d8284e6e116bcaffa7 Author: Jonathan Wakely <jwakely@redhat.com> Date: Thu Dec 9 13:54:39 2021 +0000 libstdc++: Fix std::exception_ptr regressions [PR103630] This restores support for std::make_exception_ptr<E&> and for using std::exception_ptr in C++98. Because the new non-throwing implementation needs to use std::decay to handle references the original throwing implementation is used for C++98. We also need to change the typeid expression so it doesn't yield the dynamic type when the function parameter is a reference to a polymorphic type. Otherwise the new exception object could be caught by any handler matching the dynamic type, even though the actual exception object is only a copy of the base class, sliced to the static type. libstdc++-v3/ChangeLog: PR libstdc++/103630 * libsupc++/exception_ptr.h (exception_ptr): Fix exception specifications on inline definitions. (make_exception_ptr): Decay the template parameter. Use typeid of the static type. * testsuite/18_support/exception_ptr/103630.cc: New test.
Fixed on trunk so far.
The releases/gcc-11 branch has been updated by Jonathan Wakely <redi@gcc.gnu.org>: https://gcc.gnu.org/g:84e2410c8d10d7f3ca61c3063418ace8dd8f9e35 commit r11-9835-g84e2410c8d10d7f3ca61c3063418ace8dd8f9e35 Author: Jonathan Wakely <jwakely@redhat.com> Date: Thu Dec 9 13:54:39 2021 +0000 libstdc++: Fix std::exception_ptr regressions [PR103630] This restores support for std::make_exception_ptr<E&> and for using std::exception_ptr in C++98. Because the new non-throwing implementation needs to use std::decay to handle references the original throwing implementation is used for C++98. We also need to change the typeid expression so it doesn't yield the dynamic type when the function parameter is a reference to a polymorphic type. Otherwise the new exception object could be caught by any handler matching the dynamic type, even though the actual exception object is only a copy of the base class, sliced to the static type. libstdc++-v3/ChangeLog: PR libstdc++/103630 * libsupc++/exception_ptr.h (exception_ptr): Fix exception specifications on inline definitions. (make_exception_ptr): Decay the template parameter. Use typeid of the static type. * testsuite/18_support/exception_ptr/103630.cc: New test. (cherry picked from commit a1ca039fc0fe934ef36c25d8284e6e116bcaffa7)
Fixed for 11.3 too.
The releases/gcc-10 branch has been updated by Jonathan Wakely <redi@gcc.gnu.org>: https://gcc.gnu.org/g:02b601ad7ea06460e7bc42d7251e895d9031bc6e commit r10-10570-g02b601ad7ea06460e7bc42d7251e895d9031bc6e Author: Jonathan Wakely <jwakely@redhat.com> Date: Thu Dec 9 13:54:39 2021 +0000 libstdc++: Fix std::exception_ptr regressions [PR103630] This restores support for std::make_exception_ptr<E&> and for using std::exception_ptr in C++98. Because the new non-throwing implementation needs to use std::decay to handle references the original throwing implementation is used for C++98. We also need to change the typeid expression so it doesn't yield the dynamic type when the function parameter is a reference to a polymorphic type. Otherwise the new exception object could be caught by any handler matching the dynamic type, even though the actual exception object is only a copy of the base class, sliced to the static type. libstdc++-v3/ChangeLog: PR libstdc++/103630 * libsupc++/exception_ptr.h (make_exception_ptr): Decay the template parameter. Use typeid of the static type. * testsuite/18_support/exception_ptr/103630.cc: New test. (cherry picked from commit a1ca039fc0fe934ef36c25d8284e6e116bcaffa7)
And 10.4 now.
The releases/gcc-9 branch has been updated by Jonathan Wakely <redi@gcc.gnu.org>: https://gcc.gnu.org/g:93a621991615e943704ed1a957d934736bf8a3d3 commit r9-10063-g93a621991615e943704ed1a957d934736bf8a3d3 Author: Jonathan Wakely <jwakely@redhat.com> Date: Thu Dec 9 13:54:39 2021 +0000 libstdc++: Fix std::exception_ptr regressions [PR103630] This restores support for std::make_exception_ptr<E&> and for using std::exception_ptr in C++98. Because the new non-throwing implementation needs to use std::decay to handle references the original throwing implementation is used for C++98. We also need to change the typeid expression so it doesn't yield the dynamic type when the function parameter is a reference to a polymorphic type. Otherwise the new exception object could be caught by any handler matching the dynamic type, even though the actual exception object is only a copy of the base class, sliced to the static type. libstdc++-v3/ChangeLog: PR libstdc++/103630 * libsupc++/exception_ptr.h (make_exception_ptr): Decay the template parameter. Use typeid of the static type. * testsuite/18_support/exception_ptr/103630.cc: New test. (cherry picked from commit a1ca039fc0fe934ef36c25d8284e6e116bcaffa7)
And 9.5 too.