【问题标题】:std::result_of syntax kerfufflestd::result_of 语法混乱
【发布时间】: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中添加&amp;
  • 你想要foo的返回类型(它是由lambda表达式生成的一些未命名的类)还是foo返回的lambda的返回类型?
  • @IgorTandetnik:返回类型为 foo,而不是 lambda。
  • using foo_return_type = decltype(foo(std::declval&lt;my_class&amp;&gt;(), nullptr)); 应该这样做。 Demo。不过不确定您希望将这种类型用于什么。

标签: c++ c++11 cuda result-of


【解决方案1】:

你需要一个函数指针类型和参数类型,然后将它们组合成F(ArgTypes...)的格式。例如

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

LIVE


如果你不坚持std::result_of,你也可以创建自己的类型特征。例如

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

然后

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

LIVE

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-12
    相关资源
    最近更新 更多