9

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?

YSC
  • 38,212
  • 9
  • 96
  • 149
wimalopaan
  • 4,838
  • 1
  • 21
  • 39

1 Answers1

7

Is GCC right to accept test3?

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

YSC
  • 38,212
  • 9
  • 96
  • 149
wimalopaan
  • 4,838
  • 1
  • 21
  • 39