Hold on, I have to pick my mind up off the floor because you just blew my mind right out of my skull.
I wanna talk about your code because it's amazing and how it works.
Code:
template <class F> // using a type F, awesome
auto // the compiler resolve the return type
spinLockExecutor(F& f) -> decltype(f()) // our function takes a type F and I'm assuming this lambda-ish syntax is to help the compiler interept a return type, correct?{
auto r = f();
lock.clear(std::memory_order_release);
return std::move(r); // turns out, this is actually a no-no (dangling reference, I think [plus, it kills RVO {I think}])
}
But how is this different from the code posted as a solution here? c++ - Pass a function as an explicit template parameter - Stack Overflow
Code:
#include <iostream>
using namespace std;
int multiply(int x, int y)
{
return x * y;
}
template <class F> // they do it the same way you do, King Mir
void foo(int x, int y, F f)
{
cout << f(x, y) << endl;
}
template <int (*F)(int,int)> // this is what I'm referring to
void bar(int x, int y)
{
cout << F(x, y) << endl;
}
int main()
{
foo(3, 4, multiply); // works
bar<multiply>(3, 4); //now it works!
// so, what's the main difference between these two function calls?
// is it silly to ask if one is more "performant" than the other?
return 0;
}
Edit: I'm so stupid. The way you did it is awesome for function objects, I think, while the second is awesome for function definitions, right? We can choose to pass function objects around or it seems like we can template our code with various function definitions.