template<typename T> T callWithState(auto f) { auto old = globalState; globalState = whatever(); T out = f(); globalState = old; return out; }