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

时间:2016-06-17 12:49:33

标签: 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此代码提供错误:

  

内部编译器错误:分段错误it = replacement;

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

1 个答案:

答案 0 :(得分:1)

由于分段错误而导致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
它于6月3日16日发布:https://gcc.gnu.org/gcc-5/