Skip to content

Instantly share code, notes, and snippets.

@uchan-nos
Created February 18, 2014 01:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uchan-nos/9062710 to your computer and use it in GitHub Desktop.
Save uchan-nos/9062710 to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
// {{{
constexpr int get_hash(int hash, const char* p)
{
return *p == '\0' ? hash : get_hash(*p * 137, p + 1);
}
template <typename T, int N>
class DistinctPtr
{
T* p;
public:
explicit DistinctPtr(T* p)
: p(p)
{}
operator T*() const
{
return p;
}
};
#define PTR(x) (DistinctPtr<decltype(x), get_hash(0, #x)>(&x))
// }}}
template <int N, int M>
void f(DistinctPtr<int, N> x, DistinctPtr<int, M> y)
{
static_assert(N != M, "x and y are required to be distinct");
*x = 41;
*y = 0;
if (*x == 41 && *y == 0)
{
cout << "OK" << endl;
}
else
{
cout << "FAILED" << endl;
}
}
int main(void)
{
int x, y;
f(PTR(x), PTR(x));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment