RustyX RustyX - 1 year ago 60
C++ Question

How to convert std::result_of to decltype in a template argument

At CppCon 2015, S. Lavavej from Microsoft said to avoid using

. But I have a situation in which I can't see to be able to find a proper alternative.

Consider the following code. Is there any way to change
to use

Or is this one of those situations where it's better to leave

#include <iostream>
#include <future>
#include <type_traits>

template<typename T>
struct NopErrCB
constexpr T operator() () const { return T(); }

template <typename Func, typename ErrCB>
struct SafeTaskWrapper {
Func f;
ErrCB errCB;

template <typename... T>
auto operator()(T&&... args) -> decltype(f(args...)) {
return f(std::forward<T>(args)...);
catch (...)
return errCB();

// vvv OVER HERE vvv
template <typename F>
SafeTaskWrapper<F, NopErrCB<typename std::result_of<F()>::type>> make_safe_task(F&& f) {
return { std::forward<F>(f) };

template <typename F, typename ErrCB>
SafeTaskWrapper<F, ErrCB> make_safe_task(F&& f, ErrCB&& cb) {
return { std::forward<F>(f), std::forward<ErrCB>(cb) };

int main() {
auto futA = std::async(std::launch::async, make_safe_task([] { throw "A"; return 1; }));
auto futB = std::async(std::launch::async, make_safe_task([] { throw "B"; return 1; }, [] { return 2; }));
auto futC = std::async(std::launch::async, make_safe_task([] { throw "C"; }));
std::cout << futA.get() << std::endl;
std::cout << futB.get() << std::endl;

PS. Please don't mind the purpose of the SafeTaskWrapper. I know a future already handles C++ exceptions. This is just a demo, the actual code is for handling Windows SEH exceptions, but that doesn't matter for this question.

Answer Source

You might use


instead of

typename std::result_of<F()>::type

which are similar, but have subtle differences (for SFINAE)