Skip to content

Instantly share code, notes, and snippets.

@SilverIce
Created May 16, 2015 06:14
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 SilverIce/90c124aac6df17e5ef4c to your computer and use it in GitHub Desktop.
Save SilverIce/90c124aac6df17e5ef4c to your computer and use it in GitHub Desktop.
some crazy stuff
struct Node {
};
template<class T>
struct ReadOnly {
T* _val;
friend class Writeable<T>;
public:
//ReadOnly(T& v) : _val(&v) {}
//ReadOnly(Writeable<T>& v) : _val(v.readonly()) {}
const T& value() const { assert(_val); return *_val; }
Writeable<T> writeable() {
Writeable<T> v = { _val };
_val = nullptr;
return v;
}
};
template<class T>
struct Writeable {
T* _val;
friend class ReadOnly<T>;
public:
// Can't be constructed from 'const T&' !!!
Writeable(T& v) : _val(v) {}
//Writeable(ReadOnly<T>& v) : _val(v.writeable()) {}
T& value() const { return *_val; }
ReadOnly<T> readonly() {
ReadOnly<T> v = { _val };
_val = nullptr;
return v;
}
};
void doConst(ReadOnly<Node> n) {}
void doMut(Writeable<Node> n) {}
static Writeable<Node> findMatchingNode(Writeable<Node> root) {
{
doConst(root.readonly());
}
doMut(root);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment