我在某处有一个结构:
struct A {
ComplicatedType1 f();
ComplicatedType2 f(int);
};
我想使用编译时帮助程序获取f()
的返回类型。我正在尝试std::result_of<>
:
using Type = std::result_of<decltype(&A::f)()>::type;
但是编译器给了我一个合理的错误:“无法解析对重载函数的引用”。
所以我转到SO并看到this接受和赞成的答案,建议制作static_cast<ComplicatedType1 (A::*)()>(&A::f)
- 但此时我还没有ComplicatedType1
。我陷入了递归。
如何使用最少的代码在ComplicatedType1
表达式中获取using
?
答案 0 :(得分:12)
#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");
}
编辑:更改为在Coliru上获取f()(而不是f(int))和c ++ 11(而不是c ++ 14)的返回类型