I was trying to compile a project with a code that looks like this
我试图使用看起来像这样的代码编译项目
#include <tuple>
#include <utility>
struct Foo
{
};
template <typename... Args>
void start(Args&&... args) {
auto x = [args = std::make_tuple(std::forward<Args>(args)...)] () mutable {
auto y = [args] () mutable {
auto z = [] (Args&&... args) {
return new Foo(std::forward<Args>(args)...);
};
};
};
}
int main()
{
start(Foo{});
}
It seems to compile fine in GCC 4.9.1 but not in Clang 3.4, 3.5, 3.6. The error message is
似乎在GCC 4.9.1中编译良好,但在Clang 3.4,3.5,3.6中没有编译。错误消息是
error: variable 'args' cannot be implicitly captured in a lambda with no capture-default specified
错误:变量'args'不能在没有指定capture-default的lambda中隐式捕获
Is it a compiler bug? If so, is there any workaround to get it to compile on Clang?
这是编译器错误吗?如果是这样,是否有任何解决方法让它在Clang上编译?
1 个解决方案
#1
Reduced to:
template <typename T>
void start(T t) {
[a = t] { [a]{}; };
}
int main()
{
start(0);
}
Which generates the same error in non-trunk versions of clang.
这在clang的非主干版本中会产生相同的错误。
This appears to be caused by a bug in Clang's handling of a non-init-capture of an init-capture. Said bug was fixed yesterday, May 7, and the current trunk version of Clang compiles the code above correctly.
这似乎是由Clang处理init-capture的非初始捕获错误引起的。昨天,5月7日修复了错误,当前的Clang主干版本正确编译了上面的代码。
Since this bug only manifests for non-init-captures of init-captures, a simple workaround is to use an init-capture in the inner lambda as well - instead of [a]
, do [a = a]
.
由于此错误仅表示init-capture的非初始捕获,因此一个简单的解决方法是在内部lambda中使用init-capture - 而不是[a],执行[a = a]。
#1
Reduced to:
template <typename T>
void start(T t) {
[a = t] { [a]{}; };
}
int main()
{
start(0);
}
Which generates the same error in non-trunk versions of clang.
这在clang的非主干版本中会产生相同的错误。
This appears to be caused by a bug in Clang's handling of a non-init-capture of an init-capture. Said bug was fixed yesterday, May 7, and the current trunk version of Clang compiles the code above correctly.
这似乎是由Clang处理init-capture的非初始捕获错误引起的。昨天,5月7日修复了错误,当前的Clang主干版本正确编译了上面的代码。
Since this bug only manifests for non-init-captures of init-captures, a simple workaround is to use an init-capture in the inner lambda as well - instead of [a]
, do [a = a]
.
由于此错误仅表示init-capture的非初始捕获,因此一个简单的解决方法是在内部lambda中使用init-capture - 而不是[a],执行[a = a]。