std::tuple 是否接受 auto 作为类型

Does std::tuple accept auto as a type

我必须 return 一个 int 和一个函数对象给调用者,我被认为 returning 一个像 make_tuple(int,[](some){}) 这样的元组我现在在 GCC 上,但没有支持 decltpe(auto) 作为 return 类型,有什么方法可以让我的 return 类型成为 std::tuple<int,auto> myfun() 现在我一直在做下面的事情(但不确定)

auto myfun()->decltype(make_tuple(100,p))
{
   auto p=[](some){};
   return make_tuple(100,p);
}

我这样做可以吗?

不,因为 p 在该范围内不可用,并且 lambda 表达式不应出现在未计算的上下文中(即 decltype)。 (查看更多here

但是,您可以将其包装在 std::function:

std::tuple<int, std::function<void(some)>> myfun()
{
  std::function<void(some)> p = [](some){};
  return std::forward_as_tuple(100, p);
}