简体   繁体   English

为什么我不能将[](auto &amp;&amp;…){}转换为std :: function <void()> ?

[英]Why can't I convert [](auto&&…){} into a std::function<void()>?

When I try to compile this: 当我尝试编译时:

#include <functional>

void f(std::function<void()> f)
{
}

void g()
{
  f([](auto&&...){});
}

on gcc 7.3, I get the following error: 在gcc 7.3上,出现以下错误:

[x86-64 gcc 7.3 #1] error: could not convert ' <lambda closure object>g()::<lambda(auto:1&&, ...)>{} ' from ' g()::<lambda(auto:1&&, ...)> ' to ' std::function<void()> ' [x86-64 gcc 7.3#1]错误:无法将' <lambda closure object>g()::<lambda(auto:1&&, ...)>{} '从' g()::<lambda(auto:1&&, ...)> '到' std::function<void()> '

Can someone explain why this is invalid c++? 有人可以解释为什么这是无效的c ++吗? Or should I submit a bug report? 还是应该提交错误报告? (MSVC 14 accepts and compiles it to what I expect.) (MSVC 14接受并将其编译为我所期望的。)

This is a gcc bug . 这是一个gcc错误 It interprets your lambda as follow: 它解释您的lambda如下:

[](auto&&, ...){}

So there's one argument, followed by C variadic. 因此,有一个论点,其次是C variadic。

If you add a name to your parameter pack, then it works as intended: 如果将名称添加到参数包中,则它会按预期工作:

[](auto&&... pack){}

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

相关问题 为什么 std::function 可以<void(t)>分配给 std::function<void(t const&)> 但不是 std::function<void(t&)> ?</void(t&)></void(t></void(t)> - Why can std::function<void(T)> get assigned to std::function<void(T const&)> but not std::function<void(T&)>? 为什么我不能在这里将 lambda 转换为 std::function ? - why can't I convert a lambda to a std::function here? 功能结果自动&amp;&amp; - Function result auto&& 转换 std::function<void ()> 作废 (*)()</void> - Convert std::function<void ()> to void (*)() 将void *转换为std :: function <void()> - Convert void* to std::function<void()> 为什么我不能使用auto与std :: thread? - Why can't I use auto with std::thread? 是`std::decay_t<T> decay_copy(T&amp;&amp;)` 等价于 `autodecay_copy(auto&amp;&amp;)`? - Is `std::decay_t<T> decay_copy(T&&)` equivalent to `auto decay_copy(auto&&)`? auto与auto &amp;&amp;&amp;函数返回值 - auto vs auto&& for a function return value 在具有不同签名的std :: function之间转换(T * arg到void * arg) - Convert between std::function with different signatures (T* arg to void* arg) 有没有办法将 void (*)(uint32_t, void (*)(uint32_t)) 转换为 std::function <void(uint32_t, std::function<void(uint32_t)> )&gt;?</void(uint32_t,> - Is there a way to convert void (*)(uint32_t, void (*)(uint32_t)) to std::function<void(uint32_t, std::function<void(uint32_t)>)>?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM