【发布时间】:2020-04-20 12:47:45
【问题描述】:
我有以下(CUDA)功能:
__device__ auto foo(my_class& x, my_function_ptr_type y ) {
return [gen](my_class& x) { return x.set_y(y); };
}
我想要 typedef 它的返回值的类型。我一直在摆弄 std::result_of 语法,但不能完全正确。这行不通:
using foo_return_type = std::result_of<decltype(foo(my_class{}, my_function_ptr_type{nullptr}))>::type;
也不是这样:
using foo_return_type = std::result_of<decltype(foo(my_class, my_function_ptr_type))>::type;
也不是这样:
using foo_return_type = std::result_of<foo>::type;
应该我有什么作为std::result_of 的模板参数?
注意事项:
- 命名空间中只有一个
foo()。 - 不涉及模板(
std::result_of...除外) - C++11 或 C++14,任君选择(但请注意,这是 CUDA,所以理论上这可能是个问题)。
- 编译器:NVCC 10.1
【问题讨论】:
-
删除第二个变体的
decltype,并在my_class中添加&。 -
你想要
foo的返回类型(它是由lambda表达式生成的一些未命名的类)还是foo返回的lambda的返回类型? -
@IgorTandetnik:返回类型为 foo,而不是 lambda。
-
using foo_return_type = decltype(foo(std::declval<my_class&>(), nullptr));应该这样做。 Demo。不过不确定您希望将这种类型用于什么。