Hello, and welcome to C++ Weekly. I'm your host, Jason Turner, available for code reviews
and on-site training. In this episode I want to, uh, well, I want to discuss safely using
'goto'. Now it seems like we've been taught for a very long time to not use 'goto' in C++, and
just for the record this is not a best practices episode. I am not suggesting that you use 'goto',
but let's just talk about what it means to use 'goto' in C++.
All right. Now if you saw my episode about "goto's are everywhere" (and I'll try to remember to put
a link to that somewhere in here - maybe a pop-up or something) then you're aware that the compiler
is actually generating 'goto' calls in lots of places for you, like a for loop is effectively
a 'goto' and we can create our own for loop as a 'goto' as well.
All right, so we've got a for loop that prints
one through ten. It looks like I got something wrong now doesn't it?
There we go. I need to increment before doing the check, and we should have one through nine printed
in our hand rolled for loop. So that's, first of all, one reason to not use 'goto'. The constructs
that are built into the language are you know, easier to get right, but you might ask things
like, "Well what happens if I try to jump to some place where variable hasn't been created yet?" So
let's say at the top here I want to "goto start." The compiler complains "jump to label start" from
here... blah, blah, blah. "Crosses initialization of int i." So this thing that would have been
unsafe to jump over the initialization of 'i' and its allocation on the stack space potentially
is flat out forbidden by the compiler. All right, well let's try something different.
So see here, this says "a_label is defined but not used" and "a_label is used but not defined," so
our labels are actually scoped to functions. There's no way
to jump into another function, and there's no way to jump over a variable initialization.
So the common things where goto's could cause a problem -undefined behavior,
unexpected behavior - they're just simply not allowed in C++.
So you could write code like this. This is just going to print the values 1-4, and then it's
going to goto exit. Now if you noticed while I was editing this you can't have a label on an
empty statement. The label must mean something, so that's not going to compile. So I have to have an
explicit return here, which is fair because main does return a value; but I could just as easily
have done an early return from main, or I could have done a break, or
I could have reformatted this code in such a way that whatever other logic I needed actually
existed in some other function. So I'm still going to get a compiler failure here because
I've got my warnings turned up high enough that I've got labels that are defined but not used,
and it doesn't like that. So the question of "Can you safely use goto in C++?" Yeah,
absolutely. The compiler is going to stop you from doing something that is unsafe. Is it a good idea
to use goto's in C++? Probably not. There's very rare situations where you might be able to eke out
one tiny extra little instruction of savings, but more than likely if you just refactor the
code to have better organization you don't actually have a need for goto. So this,
again, not a best practice. I'm not telling you to go and use 'goto', I'm just saying "Yeah,
technically you can safely use it if you want to create harder to maintain code and make
your co-workers angry with you." So thanks for watching this episode of C++ Weekly. You know,
do the normal things - be sure to subscribe, give this video a thumb up if you liked it.