Firstly, this signature will not compile:
// inside spinlock class template<typename F, typename... Ars> std::result_of(F(Args...)) exec(F fun, Args&&... args) { // locks the memory and then executes fun(args...) };
Return type must be
typename std::result_of<F(Args...)>::type
If your compiler implements N3436 , then this function will not participate in overload resolution, if fun(args...) not a valid expression, but this is not required in C ++ 11 and has not yet been implemented by many compilers. You will need to do your own SFINAE check to prevent result_of error message if fun(args...) invalid, or rewrite it without result_of
template<typename F, typename... Args> auto exec(F fun, Args&&... args) -> decltype(fun(std::forward<Args>(args)...)) {
Then you can overload it for functions that require an additional parameter:
template<typename F, typename... Args> auto exec(F fun, Args&&... args) -> decltype(fun(*this->shared_memory, std::forward<Args>(args)...)) {
If fun(std::forward<Args>(args)...) invalid, the first overload will not participate in overload resolution. If fun(*this->shared_memory, std::forward<Args>(args)...) invalid, the second overload will not participate in overload resolution. If neither of them is valid, the call will be poorly formed; if both are valid, the call will be ambiguous.
source share