Date: Tue, 7 Jan 2025 00:32:59 +0000
On Tue, Jan 7, 2025 at 12:13 AM Ville Voutilainen wrote:
>
> > Can anyone provide some C++ code that really needs goto?
>
> For that definition of "needed", we have never needed it, not before
> 2002 nor after, not after ChatGPT was invented
> nor before.
The following code works fine with g++, clang and VC++:
#include <iostream>
using std::cout, std::endl;
consteval int Monkey(int const arg)
{
int a = arg;
for ( unsigned i = 0u; i < 22u; ++i )
{
bool double_break = false;
for ( unsigned j = 6u; j < 12u; ++j )
{
a += i*j;
if ( 0u != (a % 8u) ) continue;
double_break = true;
break;
}
if ( double_break ) break;
}
return ++a;
}
int main(void)
{
constexpr int n = 7;
constexpr int retval = Monkey(n);
return retval;
}
Here it is on GodBolt for the three compilers:
https://godbolt.org/z/3a5EvM94x
The point I'm trying to make here is that we don't really need 'goto'
-- not at compile time and not at runtime -- I mean if you have
several nested loops and it's a few hundred lines long, then just feed
it into ChatGPT and ask it to get rid of 'goto'. Of course you'll need
to test it afterward because ChatGPT can mess up, but it's nowhere
near as much work as rewriting it all yourself -- and much much much
less work than editing a compiler to allow constexpr goto.
>
> > Can anyone provide some C++ code that really needs goto?
>
> For that definition of "needed", we have never needed it, not before
> 2002 nor after, not after ChatGPT was invented
> nor before.
The following code works fine with g++, clang and VC++:
#include <iostream>
using std::cout, std::endl;
consteval int Monkey(int const arg)
{
int a = arg;
for ( unsigned i = 0u; i < 22u; ++i )
{
bool double_break = false;
for ( unsigned j = 6u; j < 12u; ++j )
{
a += i*j;
if ( 0u != (a % 8u) ) continue;
double_break = true;
break;
}
if ( double_break ) break;
}
return ++a;
}
int main(void)
{
constexpr int n = 7;
constexpr int retval = Monkey(n);
return retval;
}
Here it is on GodBolt for the three compilers:
https://godbolt.org/z/3a5EvM94x
The point I'm trying to make here is that we don't really need 'goto'
-- not at compile time and not at runtime -- I mean if you have
several nested loops and it's a few hundred lines long, then just feed
it into ChatGPT and ask it to get rid of 'goto'. Of course you'll need
to test it afterward because ChatGPT can mess up, but it's nowhere
near as much work as rewriting it all yourself -- and much much much
less work than editing a compiler to allow constexpr goto.
Received on 2025-01-07 00:32:19