Skip to content

Instantly share code, notes, and snippets.

@maxattack
Last active April 27, 2024 20:02
Show Gist options
  • Save maxattack/17bff2f6b20df58b4a2b to your computer and use it in GitHub Desktop.
Save maxattack/17bff2f6b20df58b4a2b to your computer and use it in GitHub Desktop.
C++ Getting a Compile-Time Unique Numeric ID for a Type
#include <iostream>
template<typename T>
size_t numeric_handle() {
static int x;
return (size_t)(&x);
}
int main(int argc, char* argv[]) {
using namespace std;
cout << "int " << numeric_handle<int>() << endl;
cout << "float " << numeric_handle<float>() << endl;
return 0;
}
@colesnicov
Copy link

You just return the address of the pointer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment