function template
<memory>

std::undeclare_reachable

template <class T> T* undeclare_reachable (T* p);
Undeclare pointer as reachable
Revokes a previous declaration of reachability for the object pointed by p, and returns a safely-derived pointer to it.

If declare_reachable was called multiple times for the object pointed by p, the object is considered reachable until undeclare_reachable is called the same number of times.

Parameters

p
A pointer pointing to an object previously declared as reachable with declare_reachable.

Return value

A safely-derived pointer pointing to p. This pointer is guaranteed to compare equal to p.

This function throws no exceptions.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// declare_reachable / undeclare_reachable example
#include <iostream>
#include <memory>
#include <cstdint>

int main() {
  int * p = new int (1);    // dynamic object

  std::declare_reachable(p);

  p = (int*)((std::uintptr_t)p ^ UINTPTR_MAX);  // scrambling p

  // dynamic object not reachable by any live safely-derived pointer

  p = std::undeclare_reachable((int*)((std::uintptr_t)p ^ UINTPTR_MAX));
  // p is back again a safely-derived pointer to the dynamic object

  std::cout << "p: " << *p << '\n';
  delete p;

  return 0;
}


Output:
p: 1

See also