Simplest way to determine return type of function

后端 未结 3 729
温柔的废话
温柔的废话 2020-12-05 01:32

Given a very simple, but lengthy function, such as:

int foo(int a, int b, int c, int d) {
    return 1;
}

// using ReturnTypeOfFoo = ???

W

3条回答
  •  遥遥无期
    2020-12-05 02:26

    Most simple and concise is probably:

    template 
    R return_type_of(R(*)(Args...));
    
    using ReturnTypeOfFoo = decltype(return_type_of(foo));
    

    Note that this won't work for function objects or pointers to member functions. Just functions, that aren't overloaded or templates, or noexcept.

    But this can be extended to support all of those cases, if so desired, by adding more overloads of return_type_of.

提交回复
热议问题