Date: Fri, 11 Oct 2024 09:08:34 +0100
I wrote code yesterday to manipulate VTables:
#ifdef _MSC_VER
struct VTable {
RTTICompleteObjectLocator *locator;
void *funcptrs[1u];
};
#else
struct VTable {
std::uintptr_t top_offset;
std::type_info *p_type_info;
void *funcptrs[1u];
};
#endif
static_assert( std::is_standard_layout_v<VTable> );
constexpr std::size_t vtable_prologue_size = offsetof(VTable, funcptrs);
See how I use the 'static_assert' to be sure that the 'offsetof' won't
have undefined behaviour? Well I think the Standard should be changed
so that if you use 'offsetof' on an inappropriate type, then you get a
compile-time static_assert failure rather than undefined behaviour at
runtime.
There might be a handful of other things in the Standard that could be
changed this way too. Better to have static_assert's failing at
compile time.
#ifdef _MSC_VER
struct VTable {
RTTICompleteObjectLocator *locator;
void *funcptrs[1u];
};
#else
struct VTable {
std::uintptr_t top_offset;
std::type_info *p_type_info;
void *funcptrs[1u];
};
#endif
static_assert( std::is_standard_layout_v<VTable> );
constexpr std::size_t vtable_prologue_size = offsetof(VTable, funcptrs);
See how I use the 'static_assert' to be sure that the 'offsetof' won't
have undefined behaviour? Well I think the Standard should be changed
so that if you use 'offsetof' on an inappropriate type, then you get a
compile-time static_assert failure rather than undefined behaviour at
runtime.
There might be a handful of other things in the Standard that could be
changed this way too. Better to have static_assert's failing at
compile time.
Received on 2024-10-11 08:08:46