The least used keywords in C++, which do you use?

I

Ian Collins

Victor said:
I learned structured programming a very long time ago (remember
when it was THE way?) Anyway, we were taught to write

int func() {
int ret = 0;
/* do something */
if (!error_condition) {
/* do something else */
if (!error_condition) {
/* some more code */
ret = 1;
}
/* free some memory or whatever */
}
/* free some more memory */
return ret;
}

IIRC structured programming was nicknamed "programming without
the goto".
I guess RAII removes the need for goto in the original code, either
using exceptions or early return.
 
V

Victor Bazarov

Ian said:
I guess RAII removes the need for goto in the original code, either
using exceptions or early return.

There is the "or whatever". Of course, any action, not just
resource acquisition, can be wrapped in a destructor of some object,
but that would be as artificial as... I don't know... The 'while'
loop nonsense versus chained 'ifs' we were asked about recently.

V
 
M

Mirek Fidler

I have no problem using 'goto' but I don't remember when was the
last time I needed it. It's probably more than 10 years ago.

Interesting. You never need to break two levels of loop?

Mirek
 
W

werasm

Victor said:
You can always do it with two breaks and an if...

You can do that in many ways, but what benefit does it
give you, apart from the fact that goto is frowned upon for
other reasons. I'm playing devils advocate, but if one uses
RAII, the reasons for not using goto becomes less, not
so?

Werner
 
W

werasm

Mirek said:
Interesting. You never need to break two levels of loop?
I can't remember when last I needed to, but I did not
use goto. In retrospect I'm thinking goto in <this>
case is not such a bad idea. I've even seen an article
on it by Dr. Donald Knuth. I only skimmed it but
to me he was showing cases where goto was
in fact preferred to alternatives, and this is one of
those cases.
 
V

Victor Bazarov

werasm said:
This gives you one extra comparison for each iteration
in the outer loop, not so?

So. But unless it's known to be the bottleneck, why worry about
a mere comparison? :)

V
 
V

Victor Bazarov

werasm said:
You can do that in many ways, but what benefit does it
give you, apart from the fact that goto is frowned upon for
other reasons. I'm playing devils advocate, but if one uses
RAII, the reasons for not using goto becomes less, not
so?

So.

So? :)

V
 
M

Mirek Fidler

You can always do it with two breaks and an if...

Well, I think this is very debatable.

IMO, what really matters at the end is how maintainible is your code.
I believe that more statements usually mean more complex program logic
and more chances to make bugs.

"goto" OTOH can add complexity due to negative impact on structure.
Two gotos per single function might be too much :)

Means it boils down to what you prefer. I usually prefer goto over
additional "if"

Anyway, I think that in grand scheme of things, implementation is
usually the less important part of equation. Good interfaces (I mean,
what is in "public:" section of "class") are dominant. I think that
whole this "goto" panic is a little bit exaggerated.

BTW, I also think that "delete" is much more dangerous statement :)

Mirek
 
B

Bo Persson

Victor Bazarov wrote:
:: werasm wrote:
::: Victor Bazarov wrote:
:::: Mirek Fidler wrote:
:::::: Mirek Fidler wrote:
::::::: I have no problem using 'goto' and I use it from time to time
::::::
:::::: I have no problem using 'goto' but I don't remember when was
:::::: the last time I needed it. It's probably more than 10 years
:::::: ago.
:::::
::::: Interesting. You never need to break two levels of loop?
::::
:::: You can always do it with two breaks and an if...
:::
::: This gives you one extra comparison for each iteration
::: in the outer loop, not so?
::
:: So. But unless it's known to be the bottleneck, why worry about
:: a mere comparison? :)
::

Yes, and where did we read that using goto was a speed optimization?
:)


Bo Persson
 
O

Ole Nielsby

Pete Becker said:
Only for char. There's signed char, unsigned char, and char. The latter
must be the same type as one of the other two, but it's a distinct type.
For all other integral types, the type name alone means signed.

Where does size_t fit in? In VC8 it's unsigned, I think it is on other
systems
(haven't checked) and AFAIK there is no such thing as signed size_t.
 
P

Pete Becker

Where does size_t fit in? In VC8 it's unsigned, I think it is on other
systems
(haven't checked) and AFAIK there is no such thing as signed size_t.

size_t isn't an integral type. It's a typedef name that's a synonym for
one of the integer types.

Note also that my statement was, neverhteless, a bit too broad: bool
and wchar_t are integral types, but neither has signed or unsigned
variants.
 
J

Jorgen Grahn

....

[snip a function using goto into a chain of cleanup statements at the
end]
There is the "or whatever". Of course, any action, not just
resource acquisition, can be wrapped in a destructor of some object,
but that would be as artificial as... I don't know... The 'while'
loop nonsense versus chained 'ifs' we were asked about recently.

It doesn't have to be resources to be natural and elegant, I think. It
could be some state change on an object which (let's say) you need to
put in state A throughout the function, but must put back into its
original state (state B, maybe) before leaving it.

(But the distinction is not clear. What is a mutex lock -- a resource
or a state?)

/Jorgen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top