如何在C++中获取重载方法的返回类型?

aby*_*s.7 7 c++ overloading syntactic-sugar c++11

我有一个结构:

struct A {
  ComplicatedType1 f();
  ComplicatedType2 f(int);
};
Run Code Online (Sandbox Code Playgroud)

我想获得f()使用编译时助手的返回类型.我正在努力std::result_of<>:

using Type = std::result_of<decltype(&A::f)()>::type;
Run Code Online (Sandbox Code Playgroud)

但是编译器给了我一个合理的错误:"无法解决对重载函数的引用".

所以我去了SO,并看到了这个被接受和赞成的答案,这建议做出一个static_cast<ComplicatedType1 (A::*)()>(&A::f)- 但我现在没有ComplicatedType1.我陷入了递归之中.


如何用最少的代码ComplicatedType1进入我的using表达式?

dec*_*uto 12

这是decltype + declval的工作

#include <iostream>
#include <type_traits>
#include <utility>

struct ComplicatedType1 {};
struct ComplicatedType2 {};

struct A {
  ComplicatedType1 f();
  ComplicatedType2 f(int);
};

int main()
{
    using Type = decltype(std::declval<A>().f());
    static_assert(std::is_same<Type,ComplicatedType1>::value,"Oops");
}
Run Code Online (Sandbox Code Playgroud)

住在Coliru

编辑:改为在Coliru上获得f()(而不是f(int))和c ++ 11(而不是c ++ 14)的返回类型