Last active
December 1, 2020 09:00
-
-
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?
This file contains hidden or 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
#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