what you can learn from being too cute

1 · Jia Yuehua · May 7, 2022, 1 p.m.
daisy在其pure virtual cpp的演讲中讲解了一段非常有趣的代码 auto impl(std::string_view v = "") { static std::string stored; static int _ = (stored = v, []{ throw 0; }(), 0); __builtin_unreachable(); // since _ initializer throws return []{ return stored; }; // spooky access } void set(auto value) { try { impl(value); } catch (int) {} } auto get = decltype(impl()){}; int main() { set("hello "); std::cout << get(); set("world!\n"); std::cout << get(); } 这段代码的意思是,如果你想要设置一个值,你可以使用set函数,如果你想要获取一个值,你...