Skip to content

Instantly share code, notes, and snippets.

@dbremner
Created January 9, 2022 03:21
Show Gist options
  • Save dbremner/47ff95d7738c0cfe577eba796d2540e1 to your computer and use it in GitHub Desktop.
Save dbremner/47ff95d7738c0cfe577eba796d2540e1 to your computer and use it in GitHub Desktop.
cast wrappers for clang nullability types
/// Cast a nullable pointer to a non-null pointer
/// This is unsafe and should be justified with a comment
/// containing the keyword WHY
/// For example, "WHY - checked"
template <typename T>
[[nodiscard]] constexpr static inline auto
as_live(T *_Nullable value) -> T *_Nonnull
{
return static_cast<T *_Nonnull>(value);
}
/// Cast a const nullable pointer to a const non-null pointer
/// This is unsafe and should be justified with a comment
/// containing the keyword WHY
/// For example, "WHY - checked"
template <typename T>
[[nodiscard]] constexpr static inline auto
as_const_live(const T *_Nullable value) -> const T *_Nonnull
{
return static_cast<const T *_Nonnull>(value);
}
/// Cast a pure nullable pointer to a pure non-null pointer
/// This is unsafe and should be justified with a comment
/// containing the keyword WHY
/// For example, "WHY - checked"
template <typename T>
[[nodiscard]] constexpr static inline auto
as_pure_live(const T *const _Nullable value) -> const T *const _Nonnull
{
return static_cast<const T *const _Nonnull>(value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment