std::optional: How, when, and why - C++ Team Blog
Thu Aug 18 2022 15:51:31 GMT+0000 (Coordinated Universal Time)
Saved by
@Marcos_
using T = /* some object type */;
struct S {
bool is_initialized = false;
alignas(T) unsigned char maybe_T[sizeof(T)];
void construct_the_T(int arg) {
assert(!is_initialized);
new (&maybe_T) T(arg);
is_initialized = true;
}
T& get_the_T() {
assert(is_initialized);
return reinterpret_cast<T&>(maybe_T);
}
~S() {
if (is_initialized) {
get_the_T().~T(); // destroy the T
}
}
// ... lots of code ...
};
content_copyCOPY
Understanding how to pass nullable referances into a CLR layer for pass to the CLI converting C++ to C#
https://devblogs.microsoft.com/cppblog/stdoptional-how-when-and-why/
Comments