79756757

Date: 2025-09-05 12:15:21
Score: 0.5
Natty:
Report link

Why is this only an error in C++

As others commented above, C and C++ are different languages.

Specifically C++ is very different when it comes to creation and destruction of objects (having constructors and destructors).
Also the support for exceptions and stack unwinding makes things a lot more complicated.

Because of these (and possibly additional reasons) it makes sense to me to disallow edge cases that can complicates things unnecessarily.
And so it is invalid in C++.

is there a way I can tell GCC/Clang to allow it?

I don't think so.
But as a workaround (as @Jarod42 commented), you can enclose the definition and initialization of a in a block. This will makes it valid in C++:

int foo() {
    goto lbl;
    {   // <- block start
        int a = 4;
    }   // <- block end
lbl:
    return 5;
}

Live demo - Godbolt

Reasons:
  • Blacklisted phrase (1): is there a way
  • RegEx Blacklisted phrase (0.5): Why is this
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • User mentioned (1): @Jarod42
  • Starts with a question (0.5): Why is this on
  • High reputation (-2):
Posted by: wohlstad