clang vs gcc: variadic lambda captures

杀马特。学长 韩版系。学妹 提交于 2019-12-08 15:21:00

问题


I am trying to capture a variadic lambda argument inside a inner lambda and use it there. As an example, consider this code:

int main () {
    auto first = [&] (auto&&... one) {
        auto second = [&] (auto&&... two) {
            return ((one * two) + ...);
        };
        return second(one...);
    };
    return first(5);
}

This works with gcc9 but fails with clang8 (https://godbolt.org/z/i2K9cK).

A way to make the code compile is to explicitly capture [&one...], but i was wondering whether this is a bug in clang.

Also interesting: Changing the return statement to something where one is directly expanded (before combining with two), this compiles again: return (((one * ...) * two) + ...);

I have found this related post, but the bug declared there seems to be fixed in clang8.


回答1:


This is a bug in Clang. It has been reported. Per comment:

Fixed in r362358.

(Side note: Clang seems to have difficulty with pack-expansions in captures in general. Let's roll out our own version of the closure type generated for the generic lambda first:

struct __closure_first {
    template <typename... Args>
    auto operator()(Args&&... one) const
    {
        auto second = [&] (auto&&... two) {
            return ((one * two) + ...);
        };
        return second(one...);
    }
};

Clearly, this is not a real closure type, and non-closure local classes cannot have member function templates. Putting this in the global scope, GCC still works and Clang still fails.)



来源:https://stackoverflow.com/questions/56356708/clang-vs-gcc-variadic-lambda-captures

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!