2021年4月27日星期二

Can std::call_once be reset?

I wrote a function a while ago, based on Unreal Engine 4's blueprint implementation, that invokes a callable exactly once until it is reset:

template<typename Callable>  void DoOnce(Callable&& f, bool reset = false, bool start_closed = false) noexcept {      static bool once = start_closed;      if(!once) {          once = true;          std::invoke(f);      }      if(reset) {          once = false;      }  }  

Today I learned std::call_once exists, works across threads, works with callables that have arguments, tests for exception-safety, and basically wraps around std::invoke like mine does (the MSVC implementation at least).

That sounds great and, where possible, I prefer calling a pre-existing function over writing my own anyway.

Like Unreal's documentaion suggests, there are times that I may need to invoke the callable again by resetting the internal flag; can std::call_once be reset in order to allow the underlying callable to be invoked again?

https://stackoverflow.com/questions/67293209/can-stdcall-once-be-reset April 28, 2021 at 11:08AM

没有评论:

发表评论