0

I would add, do you know if there are any intentions to remove it in the future?

Is it because it's not so a demonized statement when you write low level code (e.g. maybe mixing C++ and assembler)?

rturrado
  • 7,699
  • 6
  • 42
  • 62
  • 12
    `goto` is still needed today. It's still in the linux kernel and it really the only way to break out of deeply nested loops/if/else chains without creating a lot of extra code. Yes, `goto` gets abused, but there are still valid use cases today. – NathanOliver Jan 01 '21 at 19:06
  • 5
    What benefit would you gain by removing goto? – 0RR Jan 01 '21 at 19:06
  • @0RR Stop people from using it, for their own safety. If it's there, it will be used. – rturrado Jan 01 '21 at 19:09
  • @NathanOliver couldn't you use exceptions for that? – rturrado Jan 01 '21 at 19:11
  • 6
    @rturrado using an exception, that is an error-handling construct with associated costs, to perform a basic flow control operation *would* be gross misuse of the language. – Quentin Jan 01 '21 at 19:15
  • 5
    The evolution of C++ seems to be more about providing "better" ways to do things and letting people choose whether to adopt them. Breaking backward compatibility just to prevent a coding style you don't like doesn't seem to be their approach. – Nate Eldredge Jan 01 '21 at 19:16
  • 4
    @rturrado Are you from Russian government? They like to forbid things. – Alexander Dyagilev Jan 01 '21 at 19:17
  • @NateEldredge I am trying to justify the presence of the sentence in the language for reasons other than backward compatibility. You can put the question other way: would you include goto if you were designing C++ from scratch? – rturrado Jan 01 '21 at 19:33
  • I think that would make the question opinion-based, as yet another iteration of the ancient war over whether `goto` is ever appropriate. – Nate Eldredge Jan 01 '21 at 19:35
  • @AlexanderDyagilev haha not unless I've been brainwashed and I don't really know. – rturrado Jan 01 '21 at 19:38
  • 3
    Note that `goto` isn't a **sentence**, it's a **statement** (and a keyword). – Blastfurnace Jan 01 '21 at 19:40
  • 1
    @rturrado You could, but then you'd need to make a exception type for each exit case which is double the code, plus IIRC, throwing an exception is more expensive then `goto`, – NathanOliver Jan 01 '21 at 19:41
  • @NathanOliver it is mainlly used in kernel because it is C not C++. With RAII, most uses of goto are really arguable. – OznOg Jan 01 '21 at 19:48
  • 3
    @OznOg RAII doesn't help with breaking out of nested loops. – HolyBlackCat Jan 01 '21 at 19:49
  • *"Stop people from using it, for their own safety"* Ha! This is not how C++ works. It tends to give programmers as much freedom as possible. – HolyBlackCat Jan 01 '21 at 19:50
  • 4
    `goto` is a tool. Like all tools it can be misused. But just because you (the generic you) don't know how to use it wisely does not mean I should be prevented from using it when I (then generic I) have a legitimate need for it (as determined by experience and profiling). Just because it is usually a bad idea does not mean that it is always a bad idea. – Martin York Jan 01 '21 at 19:52
  • 1
    Related threads about whether `goto` should be used (not specific to C++): [1](https://stackoverflow.com/q/46586/12149471) [2](https://stackoverflow.com/q/24451/12149471) [3](https://codereview.stackexchange.com/q/250656/235181) – Andreas Wenzel Jan 02 '21 at 15:27
  • @AndreasWenzel many thanks, very interesting, I see it's quite a controversial topic! – rturrado Jan 02 '21 at 16:25

3 Answers3

7

I would add, do you know if there are any intentions to remove it in the future?

No, there are not. It has never been proposed, and if it were, it would almost universally be laughed out of committee. Not just because of BC, but on the merits of the idea.

goto is a tool of marginal usefulness, but it is useful. Most new C++ programmers don't even learn how goto works in their introductory learning, so it's not like there's a rash of people abusing the feature. There's nothing to be gained by removing it, save for pandering to purists who have taken "Goto consider harmful" not as a suggestion or even as a default position, but as religious doctrine.

There are complex cases where goto is the least bad and most clear of the various control flow structures available to you. They may be rare, but they do exist, so there's nothing to be gained from removing the feature.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • Of course, the same can be said of "comefrom". Except it isn't in the language (except exceptions I suppose), so they ain't gonna add it. :) But people know more of the cases where `goto` is useful, as nobody in their right mind adds "comefrom" to a language. – Yakk - Adam Nevraumont Jan 02 '21 at 07:19
5

When writing a programs to generate a C or C++ source code to perform some task (e.g. when transpiling code from some other language into C or C++), the mix of constructs that one will output will often be quite different from the range of constructs one would use if writing C or C++ code directly. If one is writing a transpiler for a language which has some control structures that C and C++ wouldn't conveniently support without using goto, allowing a transpiler to implement such structures using goto will often make things much more convenient than requiring it to add flags or other such hackery to work around the lack of goto.

supercat
  • 77,689
  • 9
  • 166
  • 211
2

At the assembly level, there is only goto. No braces. Jmps are really gotos. C++ has always tried to make everything possible. Hence goto will not be removed. 99.99% of apps don't need it, but 0.01% do.

It's easily implemented in the compiler as well.

Besides, goto is not likely to be abused nowadays. How many students even learn there is such a keyword? And if they do notice it, they are unlikely to know where to use it. Goto was dangerous back in the spaghetti coding days, but not today.

Michael Chourdakis
  • 10,345
  • 3
  • 42
  • 78