Macro Subspace :: sus_class_trivially_relocatable_if

#define sus_class_trivially_relocatable_if(unsafe_fn, is_trivially_reloc)

Mark a class as trivially relocatable based on a compile-time condition.

This macro is most useful in templates where the condition is based on the template parameters.

Avoid marking the class with the [[clang::trivial_abi]] attribute, as when the class is not trivially relocatable (the value is false), it can cause memory safety bugs and Undefined Behaviour.

Use the TriviallyRelocatable concept to determine if a type is trivially relocatable, and to test with static_assert that types are matching what you are expecting. This allows containers to optimize their implementations when relocating the type in memory.

Macro Style
sus_class_trivially_relocatable asserts all param types are trivially relocatable
sus_class_trivially_relocatable_if_types is conditionally trivially relocatable if all param types are
sus_class_trivially_relocatable_if is conditionally trivially relocatable if the condition is true
sus_class_trivially_relocatable_unchecked is trivially relocatable without any condition or assertion

Example

template <class T>
struct S {
  Thing<i32> thing;
  i32 more;

  sus_class_trivially_relocatable_if(
      unsafe_fn,
      SomeCondition<Thing<T>>
      && sus::mem::TriviallyRelocatable<decltype(thing)>
      && sus::mem::TriviallyRelocatable<decltype(more)>);
};