c++ - 从捕获的值变量分配给 Lambda 参数时出现 gcc 编译器段错误

标签 c++ gcc lambda capture gcc5

我正在研究 this answer并编写了代码:

bool generate(vector<int>& temp, int& target, const size_t width, const size_t i) {
    const auto replacement = temp[i];
    const auto result = target > replacement;

    if (result) {
        for_each(begin(temp), next(begin(temp), min(temp.size(), i + width - 1)), [=](auto& it) {
            if (target == it) {
                it = replacement;
            } else if (target < it) {
                --it;
            } });
    }
    target = replacement;
    return result;
}

int main() {
    const vector<char> rooms = { 0b1101,    0b110,  0b1101, 0b110,  0b1100, 0b101,  0b110,
                                 0b1110,    0b1001, 0b110,  0b1011, 0b1010, 0b1111, 0b1010,
                                 0b1000,    0b101,  0b11,   0b1110, 0b1011, 0b1110, 0b1010,
                                 0b1011,    0b1101, 0b101,  0b1,    0b101,  0b11,   0b1011 };
    const size_t width = 7U;
    auto result = 0;
    vector<int> temp(rooms.size());

    for (size_t i = 0U; i < rooms.size(); ++i) {
        const auto toWest = (rooms[i] & 0b1000) == 0;
        const auto toNorth = (rooms[i] & 0b100) == 0;
        const auto toEast = (rooms[i] & 0b10) == 0;
        const auto toSouth = (rooms[i] & 0b1) == 0;
        const auto west = toWest && temp[i - 1] != 0 ? temp[i - 1] : numeric_limits<int>::max();
        const auto north = toNorth && temp[i - width] != 0 ? temp[i - width] : numeric_limits<int>::max();
        const auto east = toEast && temp[i + 1] != 0 ? temp[i + 1] : numeric_limits<int>::max();

        temp[i] = min({ temp[i] != 0 ? temp[i] : numeric_limits<int>::max(), result + 1, west, north, east });

        if (temp[i] == result + 1) ++result;

        if (toWest) result -= generate(temp, temp[i - 1], width, i);
        if (toNorth) result -= generate(temp, temp[i - width], width, i);
        if (toEast) result -= generate(temp, temp[i + 1], width, i);
        if (toSouth) temp[i + width] = temp[i];
    }

    for (auto it = cbegin(temp); it != cend(temp);) {
        for (auto i = 0; i < width; ++i, ++it) cout << *it << '\t';
        cout << endl;
    }

    cout << endl << result << endl;
}

When compiled on gcc此代码给出了错误:

Internal compiler error: Segmentation fault it = replacement;

我已经在 gcc 5.1、5.2 和 5.3 上本地尝试过它,它在所有这些上都给出了相同的编译器错误。 Clang seems happy with the code以及 Visual Studio。这只是一个编译器错误,不是我犯的错误,对吧?

最佳答案

在 lambda 中按值捕获常量变量以及随后由于段错误导致的内部编译器错误是 gcc 5.1 - 5.3 中的错误: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70691

如果您需要修复此错误的云编译器,您可以在 http://coliru.stacked-crooked.com 上使用 gcc因为它使用的是 gcc 6.1 which has resolved the issue

gcc 5.4 也应该更正这个:https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70691#c5
它于 16 年 6 月 3 日发布:https://gcc.gnu.org/gcc-5/

关于c++ - 从捕获的值变量分配给 Lambda 参数时出现 gcc 编译器段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37881996/

相关文章:

c++ - std::bad_alloc 不进入交换空间

c - 用C语言从M68k ram地址中获取特定的十六进制

c++ - 在GDB中操作以$开头的C++成员变量

C++ Hello World : Undefined symbols for architecture x86_64

c++ - 如何保存 lambda 以供以后回调?

c++ - openCV 中的 cvBlob 和 cvBlobLib

c# - SharpDx:设置字符宽度

c# - 修改动态属性选择器 Lambda

.net - 编写 .NET 以外的 Windows 客户端应用程序的最佳方法是什么?

c++ - 使用非参数模板参数调用 lambda