Date: Tue, 16 Apr 2024 11:26:41 +0200
Yes, at runtime in can be detected, but not at compile-time.
dynamic_cast can (at least sometimes or always) detect this at compile-time.
<source>:12:5: error: ambiguous conversion from derived class 'D' to base class 'A':
class D -> B -> A
class D -> C -> A
12 | dynamic_cast<A&>(d);
| ^~~~~~~~~~~~~~~~~~~
1 error generated.
-----Ursprüngliche Nachricht-----
Von:Frederick Virchanza Gotham via Std-Proposals <std-proposals_at_[hidden]>
Gesendet:Di 16.04.2024 11:19
Betreff:Re: [std-proposals] std::any::base
An:std-proposals_at_[hidden];
CC:Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>;
On Mon, Apr 15, 2024 at 12:02 PM Sebastian Wittmeier wrote
>
> Does the base function throw, if the base is not unique for the derived class object in any?
>
> This condition cannot be detected at compile-time, as the dynamic type of the object in any (and therefore its ancestry relationship) is not known.
This can be confirmed to be possible at runtime by throwing an
exception, because the compiler can throw an exception knowing only
the object's type_info:
GodBolt: https://godbolt.org/z/xasKvr1eY
In the following code, the exception is not caught in the "A" catch
block -- which is what we want. And so in this instance,
"std::any::base<T>" would return a nullptr or throw an exception.
And copy-pasted:
#include <cstdint>
#include <iomanip>
#include <iostream>
using namespace std;
struct A { int i, j; };
struct B { float a, b; };
struct C : A, B { double c; };
struct D : A, C { void *p; };
int main(void)
{
cout << "First line\n";
try
{
D d;
throw d;
}
catch (A &a)
{
cout << "caught A\n";
}
catch (...)
{
cout << "not caught\n";
}
cout << "Last line\n";
}
--
Std-Proposals mailing list
Std-Proposals_at_[hidden]
https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
Received on 2024-04-16 09:26:44