I want to store a pointer into a fundamental data type.
My intuition says that the right data type for this is a std::size_t
and it works:
#include <fmt/core.h>
int main() {
int i = 0;
int* p = &i;
auto const t = reinterpret_cast<std::size_t>(p);
fmt::print("size of address: {}\n", sizeof(p));
fmt::print("size of std::size_t: {}\n", sizeof(t));
fmt::print("value of address to i: {}\n", static_cast<void*>(p));
fmt::print("value of std::size_t t: {0:#x}\n", t);
return *p;
}
And the program outputs:
Program returned: 0
size of address: 8
size of std::size_t: 8
value of address to i: 0x7ffdd754a0dc
value of std::size_t t: 0x7ffdd754a0dc
But is this true for every platform?
How can I be sure?