Simplest way to determine return type of function

后端 未结 3 727
温柔的废话
温柔的废话 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:12

    I don't know if is the simplest way (if you can use C++17 surely isn't: see NathanOliver's answer) but... what about declaring a function as follows:

    template 
    R getRetType (R(*)(Args...));
    

    and using decltype()?

    using ReturnTypeOfFoo = decltype( getRetType(&foo) );
    

    Observe that getRetType() is only declared and not defined because is called only a decltype(), so only the returned type is relevant.

提交回复
热议问题