Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCC fails to report an ill-formed constexpr lambda call

Below are two test-cases for Undefined Behaviour, expressed as IIFE (Immediately Called 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() {}

When compiled with GCC trunk, test4 is correctly rejected as it exhibs Undefined Behaviour in a constexpr. On the other hand test3 is accepted.

Is GCC right to accept test3?

like image 640
wimalopaan Avatar asked Jan 22 '20 15:01

wimalopaan


1 Answers

Is GCC right to accept test3?

No, this is a GCC bug. I just reported it as bug #93389.

like image 91
wimalopaan Avatar answered Nov 03 '22 00:11

wimalopaan