| 1 |
#pragma once |
| 2 |
|
| 3 |
#include <type_traits> |
| 4 |
|
| 5 |
template <typename T> |
| 6 |
class exact_type |
| 7 |
{ |
| 8 |
T *p; |
| 9 |
public: |
| 10 |
operator bool() const = delete; |
| 11 |
// Conversion to void* variants is prohibited |
| 12 |
operator void *() const = delete; |
| 13 |
operator volatile void *() const = delete; |
| 14 |
operator const void *() const = delete; |
| 15 |
operator const volatile void *() const = delete; |
| 16 |
bool operator<(auto &&) const = delete; |
| 17 |
bool operator<=(auto &&) const = delete; |
| 18 |
bool operator>(auto &&) const = delete; |
| 19 |
bool operator>=(auto &&) const = delete; |
| 20 |
constexpr exact_type(T *t) : p(t) {} |
| 21 |
// Conversion to the exact type is permitted |
| 22 |
[[nodiscard]] |
| 23 |
constexpr operator const T *() const { return p; } |
| 24 |
[[nodiscard]] |
| 25 |
constexpr operator typename std::remove_const<T>::type *() const { return p; } |
| 26 |
[[nodiscard]] |
| 27 |
constexpr bool operator==(const T *rhs) const { return p == rhs; } |
| 28 |
[[nodiscard]] |
| 29 |
constexpr bool operator==(const exact_type<T> &rhs) const = default; |
| 30 |
}; |
| 31 |
|
| 32 |
template <typename T> |
| 33 |
class prohibit_void_ptr |
| 34 |
{ |
| 35 |
public: |
| 36 |
// Return a proxy when the address is taken |
| 37 |
[[nodiscard]] |
| 38 |
constexpr exact_type<T> operator&() { return static_cast<T*>(this); } |
| 39 |
[[nodiscard]] |
| 40 |
constexpr exact_type<T const> operator&() const { return static_cast<T const*>(this); } |
| 41 |
}; |