Date: Sat, 4 Jan 2025 21:26:23 +0000
Hello everyone. I hava a small and maybe useless question about the cplusplus standard. Maybe there are some experts can help me. Here is the code.
#include <iostream>
struct A
{
A(){} // not trivial
~A(){}
void f1()const
{
std::cout << this << '\n';
}
void f2(this const A& self)
{
std::cout << &self << '\n';
}
};
void f3(const A& a)
{
std::cout << &a << '\n';
}
int main()
{
A a{};
a.~A();
a.f1(); // UB!
f3(a); // OK
a.f2(); // UB or not?
auto p = &A::f2;
p(a); // UB or not?
p = f3;
p(a); // OK
new(std::addressof(a))A{}; // OK
}
#include <iostream>
struct A
{
A(){} // not trivial
~A(){}
void f1()const
{
std::cout << this << '\n';
}
void f2(this const A& self)
{
std::cout << &self << '\n';
}
};
void f3(const A& a)
{
std::cout << &a << '\n';
}
int main()
{
A a{};
a.~A();
a.f1(); // UB!
f3(a); // OK
a.f2(); // UB or not?
auto p = &A::f2;
p(a); // UB or not?
p = f3;
p(a); // OK
new(std::addressof(a))A{}; // OK
}
Received on 2025-01-04 21:26:29