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

J

James Kanze

* James Kanze:
I actually use wchar_t and have used wchar_t a lot. :)
It plays about the same rôle as int, the platform's "natural" wide
character type.

I guess part of the problem is that there is no generally
established convention as to what a "natural" wide character
type should support.
And it has the same problems: some things are more portable with wchar_t
(such as std::wstring for holding platform-specific wide character
strings), and some things are less portable with wchar_t (such as code
that assumes a specific encoding, or other size-dependent assumption).
And the same issues seem to hold for plain char.

To some degree. The difference is that when I use char, I
expect to be dealing with different encodings. The usual reason
for going beyond char is to have a specific encoding (Unicode),
and to avoid multi-byte encodings.

In the future, of course, we'll have char32_t.
On a platform with 16-bit char I imagine one wouldn't use
std::string for holding a narrow character string.

Sure you would. On a platform with 16-bit char, there's no
smaller type available.
From a practical point of view: there are three sizes of
characters: 8 bit, 16 bit and 32 bit. (8 bit also encompasses
smaller encodings, like 7 bit ASCII. But these aren't very
relevant today.) And you have to know which one you're dealing
with. char is guaranteed to be at least 8 bit, so it's fine for
that. (Not completely, since char can be signed, which
introduces some problems. But it's usable.) wchar_t is also
guaranteed to be at least 8 bits, which is useless. In
practice, wchar_t can probably be counted on to be at least 16
bits on most general purpose platforms, so it can probably be
used for the 16 bit characters, but the 16 bit ones are the ones
I use the least. But what about the 32 bit ones? I currently
use uint32_t.
 
J

James Kanze

How about 'signed'?
[/QUOTE]
How about virtual for class inheritance

I don't think I've ever written an application without some
virtual inheritance. (Not that that's relevant to Alf's
question; he asked about keywords, not features. And the
keyword virtual certainly serves.)
 
Z

Zara

It depends on what level you're programming on. I tend to
prefer separate modules, written in assembler, to asm, but I do
use assembler for certain very low level threading primitives
(atomic increment, etc.), on some machines.



I don't use volatile today, but I worked on embedded systems in
the past, and it found use there.

In embedded design, volatile is used when definig an struc serving as
interface to hardware. I use it a lot.
And typeid pops up a lot in
experiments and demo programs (although it is practically unused
in applications).

Lucky you! When you use a not-enough-standar-complying compiler (let´s
say, CodeGear/Borland) and you want to use boost/variant, you are
forced to use typeid from time to time. But I agree, it is not a
required use for most compilers

Best regards,

zara
 
A

Alf P. Steinbach

* James Kanze:
Sure you would. On a platform with 16-bit char, there's no
smaller type available.

Oh there is. There's always the bit. As I wrote I have no experience
with such platforms, but if memory was tight I would at least consider
doing things like

typedef unsigned char UChar;

class DoubleChar
{
STATIC_ASSERT( CHAR_BITS >= 16 );
private:
UChar myValues;
public:
static char firstOf( char c ) { return c && 0xFF; }
static char secondOf( char c ) { return (c >> 8) & 0xFF; }

DoubleChar( char v1, char v2 )
: myValues( UChar(v1) | (UChar(v2) << 8) )

char operator[]( ptrdiff_t i ) const
{ return (i == 0? firstOf( myValues ) : secondOf( myValues ); }

char value() const { return myValues; }
};

and then store these double-chars packed in an array with

char operator[]( ptrdiff_t i ) const
{ return myDoubleValues[i/2][i%2]; }

From a practical point of view: there are three sizes of
characters: 8 bit, 16 bit and 32 bit. (8 bit also encompasses
smaller encodings, like 7 bit ASCII. But these aren't very
relevant today.) And you have to know which one you're dealing
with. char is guaranteed to be at least 8 bit, so it's fine for
that. (Not completely, since char can be signed, which
introduces some problems. But it's usable.) wchar_t is also
guaranteed to be at least 8 bits, which is useless. In
practice, wchar_t can probably be counted on to be at least 16
bits on most general purpose platforms, so it can probably be
used for the 16 bit characters, but the 16 bit ones are the ones
I use the least. But what about the 32 bit ones? I currently
use uint32_t.

OK, I think that's a very clear exposition. :)

Do you have any type advice for my hobby string project (now on ice for
a week or so), at <url: http://alfsstringvalue.sourceforge.net/>?

I think I'd like to get things type-wise Right(TM) before putting out a
release...

Cheers,

- Alf
 
M

Mirek Fidler

I don't recall the last time I used 'goto'.

I have no problem using 'goto' and I use it from time to time (quick
scan of my codebase reveals one goto per 5000 lines...).

'delete' is on my list of rarely used reserved words rigth after the
goto. I use it only when interfacing with libs.

Mirek
 
S

Sarath

I have no problem using 'goto' and I use it from time to time (quick
scan of my codebase reveals one goto per 5000 lines...).

'delete' is on my list of rarely used reserved words rigth after the
goto. I use it only when interfacing with libs.

Mirek

May be the least or never used keyword is goto :)

In a way using switch case is also something like goto :D

Regards,
Sarath
 
E

Earl Purple

Unless you're doing very low level programming, reinterpret_cast probably remains unused as
well.

Or using a C interface that normally involves a strange cast, eg dlsym
which returns void * as a pointer to anything, even a function, or
"pointer to pointer" which is seen as a different level of indirection
by static_cast even though it really is a pointer to something.
And of course, I've never used continue or goto, in over 25
years of C/C++. And when I encounter it in code I have to work
on, I simply throw the code out and start over.

I have never used goto but continue isn't really evil, any more than
break is. Yes you can put in a block to the end of the loop that is
not called but continue is simpler. I would say that not using
continue is a style issue only. I also sometimes return from the
middle of functions. Maybe you consider that evil too.

Having said that I couldn't find an instance of "continue" used in my
own code.

There is no logical xor as far as I'm aware, so why isn't the function
called bit_xor which shows which group it belongs in.

In logical terms, logical_xor( 1, 2 ) = false (they are both non-zero)
while logical_and( 1, 2 ) = true (same reason). In bitwise terms
bitwise_xor( 1, 2 ) = 3 while bitwise_and( 1, 2 ) = 0

If without the prefixes, and and or mean the logical variety then xor
should mean the same.
 
V

Victor Bazarov

Sarath said:
[..]
In a way using switch case is also something like goto :D

Remember the arithmetic goto in Fortran?

Actually, switch/case is more like using a jump table (which is
probably not possible in C++, it doesn't have arrays of labels).

V
 
M

Michal Nazarewicz

Alf P. Steinbach said:
Here's the list of keywords and reserved words (the difference in C++
escapes me!) that I don't use:

export, auto, register, and, and_eq, or, or_eq, xor, xor_eq, not,
not_eq, bitand, bitor, compl

How about you?

Also signed.
 
B

Barry

Michal said:
Also signed.

I only do C++ programming only on windows,
integral types are all signed if they are not specified with /unsigned/
on VC++. so here I don't use that keyword either.
AFAIK, signed/unsigned as default is implementation defined. So I guess
there's some implementation uses unsigned as default.
 
P

Pete Becker

I only do C++ programming only on windows,
integral types are all signed if they are not specified with /unsigned/
on VC++. so here I don't use that keyword either.
AFAIK, signed/unsigned as default is implementation defined. So I guess
there's some implementation uses unsigned as default.

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.
 
P

Pete Becker

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.

Whoops. The latter must have the same underlying representation as one
of the other two, but it's a distinct type.
 
B

Barry

Barry said:
I only do C++ programming only on windows,
integral types are all signed if they are not specified with /unsigned/
on VC++. so here I don't use that keyword either.
AFAIK, signed/unsigned as default is implementation defined. So I guess
there's some implementation uses unsigned as default.

Sorry, only char type
 
J

Juha Nieminen

Mirek said:
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.
 
J

Juha Nieminen

Pete said:
Whoops. The latter must have the same underlying representation as one
of the other two, but it's a distinct type.

You mean I can overload void foo(signed char), void foo(unsigned char)
and void foo(char)?
 
M

Michal Nazarewicz

Victor Bazarov said:
I don't recall the last time I used 'goto'.

I suppose goto has more use in C then in C++, ie. I've been recently
using it in the following scheme:

#v+
int func() {
int ret = 0;

/* do something */
if (error_condition) goto finish1;

/* do something else */
if (error_condition) goto finish2;

/* some more code (ie. main loop) */
ret = 1;

finish2:
/* free some memory or whatever */
finish1:
/* free some more memory */

return ret;
}
#v-

This probably can be accomplished in C++ without the use of goto
statement using destructors.
 
V

Victor Bazarov

Michal said:
I suppose goto has more use in C then in C++, ie. I've been recently
using it in the following scheme:

#v+
int func() {
int ret = 0;

/* do something */
if (error_condition) goto finish1;

/* do something else */
if (error_condition) goto finish2;

/* some more code (ie. main loop) */
ret = 1;

finish2:
/* free some memory or whatever */
finish1:
/* free some more memory */

return ret;
}
#v-

This probably can be accomplished in C++ without the use of goto
statement using destructors.

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".

V
 

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,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top