简体   繁体   English

std::result_of 语法混乱

[英]std::result_of syntax kerfuffle

I have the following (CUDA) function:我有以下(CUDA)功能:

__device__ auto foo(my_class& x, my_function_ptr_type y ) {
    return [gen](my_class& x) { return x.set_y(y); };
}

And I want typedef its return value's type.我想要 typedef 其返回值的类型。 I've fiddling with the std::result_of syntax, and can't get it quite right.我摆弄 std::result_of 语法,但不能完全正确。 This won't work:这行不通:

using foo_return_type = std::result_of<decltype(foo(my_class{}, my_function_ptr_type{nullptr}))>::type;

Nor this:也不是这个:

using foo_return_type = std::result_of<decltype(foo(my_class, my_function_ptr_type))>::type;

Nor this:也不是这个:

using foo_return_type = std::result_of<foo>::type;

What should I have as the template-argument to std::result_of ?应该有什么作为std::result_of的模板参数?

Notes:笔记:

  • There's only one foo() in the namespace.命名空间中只有一个foo()
  • No templates are involved (other than std::result_of ...)不涉及模板(除了std::result_of ...)
  • C++11 or C++14, take your pick (but note that this is CUDA, so theoretically that could be an issue). C++11 或 C++14,随你选择(但请注意,这是 CUDA,所以理论上这可能是一个问题)。
  • Compiler: NVCC 10.1编译器:NVCC 10.1

You need a function pointer type and the parameter types, then combine them in the format of F(ArgTypes...) .您需要一个函数指针类型和参数类型,然后以F(ArgTypes...)的格式将它们组合起来。 eg例如

using foo_return_type = std::result_of<decltype(&foo)(my_class&, my_function_ptr_type)>::type;

LIVE居住


You can also make your own type trait, if you don't stick to std::result_of .如果您不坚持使用std::result_of ,您也可以创建自己的类型特征。 eg例如

template <typename F>
struct return_type_of_function {};
template <typename R, typename... Args>
struct return_type_of_function<R(Args...)> {
    using type = R;
};

then然后

using foo_return_type = return_type_of_function<decltype(foo)>::type;

LIVE居住

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

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