简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM