繁体   English   中英

具有invoke_result的重载命名非成员函数的返回类型

[英]Return type of overloaded named non-member function with invoke_result

给定许多带有不同类型参数的重载函数,是否有办法在编译时在模板化上下文中获取特定重载的返回类型或参数类型之一? 例如,考虑以下情况,其中重载函数将参数引用作为输出值:

struct struct_a { };
struct struct_b { };
struct struct_c { };

float process(struct_a) { return 5.0f;  }
bool  process(struct_b) { return true; }
int   process(struct_c) { return -1; }

void process_in_place(struct_a, float& out) { out = 5.0f;  }
void process_in_place(struct_b, bool& out) { out = true;  }
void process_in_place(struct_c, int& out) { out = -1;  }

template<typename T>
void do_process(T val)
{
    auto result1 = process(val);
    std::cout << result1 << std::endl;

    // ???
    //using post_process_type = std::invoke_result_t<decltype(process)&(std::declval<T>)>;
    // ???
    post_process_type result2;
    process_in_place(val, result2);
}

int main()
{
    do_process(struct_a());
    do_process(struct_b());
    do_process(struct_c());
}

在最终选择哪个版本的process_in_place之前,我怎么知道out-param应该是哪种类型?

using post_process_type = decltype(process(std::declval<T>()));

这样就可以了。 在重载解析选择了正确的process之后, decltype只会查看返回类型。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM