c++ - GCC无法报告格式错误的constexpr lambda调用

标签 c++ undefined-behavior constexpr avr-gcc

以下是两个未定义行为的测试用例,表示为IIFE(即所谓的Lambda-Axpression):

constexpr auto test3 = []{
    int* p{};
    {   
        int x{};
        p = &x;
    }
    return *p; // Undefined Behaviour
}(); // IIFE

constexpr auto test4 = []{
    int x = std::numeric_limits<int>::min();
    int y = -x;  // Undefined Behaviour
    return y;
}();

int main() {}

使用GCC主干编译时,由于test4会显示constexpr中的Undefined Behavior,因此会被正确拒绝。另一方面,test3被接受。

GCC有权接受test3吗?

最佳答案

Is GCC right to accept test3?



不,这是一个GCC错误。我只是将其报告为bug #93389

关于c++ - GCC无法报告格式错误的constexpr lambda调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59862914/

相关文章:

c++ - 为什么 `std::invoke` 不是 constexpr?

c++ - 使用 CRTP 确定性地生成代码

c++ - 错误: 'ALIGN' undeclared (first use in this function) with ALIGN defined into macro

c++ - 双循环变量 : unspecified or undefined? 的相等条件

c++ - 重复使用 prefix++ 运算符时的未定义行为

c++ - Lambda 捕获这个和长时间运行的函数

c++ - 为什么必须在运行时构造字符串?

c++ - 我可以多次调用 JNI_CreateJavaVM 吗?

c++ - C++程序使用类模板模拟FIFO,出队时返回值3221225477

c++ - 如何迭代/计数 multimap<string,string>