Date: Tue, 16 Apr 2024 10:18:53 +0100
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";
}
>
> 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";
}
Received on 2024-04-16 09:19:06