【发布时间】:2019-05-09 10:49:22
【问题描述】:
给定一个非常简单但冗长的函数,例如:
int foo(int a, int b, int c, int d) {
return 1;
}
// using ReturnTypeOfFoo = ???
在编译时确定函数的返回类型(ReturnTypeOfFoo,在本例中为:int)最简单、最简洁的方法是什么无需重复函数的参数类型(仅按名称,因为已知该函数没有任何额外的重载)?
【问题讨论】:
-
取决于编译器或您可用的支持库...docs.microsoft.com/en-us/cpp/cpp/attributes?view=vs-2017,可以肯定的是,Concepts 也有一些 API 允许您在运行时或编译时推断此信息。跨度>
-
可能是
decltype(foo)::result_type? -
@ThomasLang 这是从哪里来的?
decltype(foo)是一个没有result_type成员的函数,还是我错过了什么? -
@user463035818 你可能就在这里,我指的是
std::function类型的result_type成员。
标签: c++ function c++17 return-type compile-time