Skip to content

Instantly share code, notes, and snippets.

@dwilliamson
Last active December 1, 2020 09:00
Show Gist options
  • Save dwilliamson/6d22da7d3ac564616d31 to your computer and use it in GitHub Desktop.
Save dwilliamson/6d22da7d3ac564616d31 to your computer and use it in GitHub Desktop.
Can you force C++ call sites to prefix "out" for all mutate-via-reference parameters?
#define out OutCatcher() <<
template <typename TYPE> struct Out
{
explicit Out(TYPE& ref) : ref(ref) { }
TYPE& ref;
};
struct OutCatcher
{
template <typename TYPE> Out<TYPE> operator << (TYPE& obj)
{
return Out<TYPE>(obj);
}
};
void Mutate(Out<Obj> obj)
{
obj.ref.x = 3;
}
void Test()
{
Obj obj;
Mutate(obj); // compile error
Mutate(out obj); // fine
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment