简体   繁体   English

MSVC 是否有解决此代码的“内部编译器错误”的方法?

[英]Is there any workaround for MSVC producing `Internal compiler error` for this code?

For some reason the below code gives me fatal error C1001: Internal compiler error.出于某种原因,下面的代码给了我fatal error C1001: Internal compiler error. with MSVC 19.27, but not with Clang .与 MSVC 19.27,但不是与 Clang Any idea how to write it so that the static_assert can be done also on MSVC?知道如何编写它以便static_assert也可以在 MSVC 上完成吗?

template <typename T, int N, typename K, int M>
constexpr int countIdentifersNotInArray(const T(&identifiers)[N], const K(&array)[M]) {

 auto find = [&array](const unsigned char value) {
        for (const auto& a : array) {
            if (a == value) {
                return true;
            }
        }
        return false;
    };

    int count = 0;
    for (const auto& value : identifiers) {
        if (!find(value)) {
            ++count;
        }
    }
    return count;
}

constexpr bool testIt() {
    return countIdentifersNotInArray({ 0x01, 0x02 }, { 0x01 });
}


int main() {
    static_assert(testIt());
    return 0;
}

I would like to use this in a an environment where stl is not available, so solutions without that are most interesting.我想在 stl 不可用的环境中使用它,所以没有它的解决方案最有趣。

As the comment has pointed out, this is an MSVC bug and you should definitely report to Microsoft.正如评论指出的那样,这是一个 MSVC 错误,您绝对应该向 Microsoft 报告。

By removing multiple lines until it stops crashing the compiler, I believe the cause is the range-for loops.通过删除多行直到它停止使编译器崩溃,我相信原因是 range-for 循环。 So, since they're arrays with known size, you can workaround with the classic indexed loops:因此,由于它们是大小已知的数组,您可以使用经典的索引循环来解决:

template <typename T, int N, typename K, int M>
constexpr int countIdentifersNotInArray(const T(&identifiers)[N], const K(&array)[M]) {

    auto find = [&array](const auto value) {
        for (int i = 0; i < M; i++) {
            if (array[i] == value) {
                return true;
            }
        }
        return false;
    };

    int count = 0;
    for (int i = 0; i < N; i++) {
        if (!find(identifiers[i])) {
            ++count;
        }
    }
    return count;
}

It works on MSVC .它适用于 MSVC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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