Hey, thanks! I was thinking that, actually. Functors are pretty cool. I like this std::function thing though.
Part of me is very surprised that std::function is part of C++11. I assumed it was old.
Hey, thanks! I was thinking that, actually. Functors are pretty cool. I like this std::function thing though.
Part of me is very surprised that std::function is part of C++11. I assumed it was old.
Okay, a quick question:
What if f() has the following declaration:Code:std::atomic_flag lock = ATOMIC_FLAG_INIT; template <class F> auto spinLockExecutor(F& f) -> decltype(f()) { while (lock.test_and_set(std::memory_order_acquire)) { } auto && r = f(); lock.clear(std::memory_order_release); return r; }?Code:void f(void)
My compiler is yelling at me about returning a reference to void which makes sense. How do I overcome this? The universal reference collapse seems to turn void into a reference.
Separate the cases when the return type is void and when not. Not tested code, but:
Code:template<typename T> struct Tag {}; // Helper type that's default-constructible template<typename F> void SpinLockExecutorHelper(F f, Tag<void>) -> void { // Implement me } template<typename F, typename R> void SpinLockExecutorHelper(F f, Tag<R>) -> R { // Implement me } template<typename F> void SpinLockExecutor(F f) -> decltype(f()) { return SpinLockExecutorHelper(f, Tag<decltype(f())>()); }
So, I haven't run the code yet but what you gave me 100% compiles squeaky clean. And it makes a lot of sense too. That was a really quick response with a good approach to when templating code gets a little bit trickier.
I really appreciate the help, you guys!
Now my code is gonna be, like, super amazing XD