Created
February 18, 2014 01:16
-
-
Save uchan-nos/9062710 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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