Nat
Nat

Reputation: 3717

Compilation error with template and lambda with Clang

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

error: variable 'args' cannot be implicitly captured in a lambda with no capture-default specified

Is it a compiler bug? If so, is there any workaround to get it to compile on Clang?

Upvotes: 1

Views: 196

Answers (1)

T.C.
T.C.

Reputation: 137315

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.

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.

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].

Upvotes: 5

Related Questions