K
Keith Thompson
Free Willy said:Keith Thompson writes: [...]Always remember that C and C++ are two different languages. The C++
semantics for "register" differ from the C semantics.
<OT>
C++ 2003 7.1.1p3:
A register specifier has the same semantics as an auto specifier
together with a hint to the implementation that the object so
declared will be heavily used. [Note: the hint can be ignored and in
most implementations it will be ignored if the address of the object
is taken. —end note]
</OT>
That's interesting, although I don't think it really explains it - I
normally use g++ in C mode because it gives some extra type checking, eg
prevents implicit typecasting of pointers.
I don't believe g++ has a C mode. g++ and gcc are part of the same
compiler collection; g++ is the C++ compiler, and gcc is the C compiler.
I suppose you mean that you use g++ to compile C code, but as you've
seen, this doesn't always work.
(OH, and you mean "implicit conversions". Casts are always explicit.)
Personally I think the C++ way is better than the C way - it avoids
problems if you try to assign more register variables than the machine
has registers available, effectively in C++ it sounds like the compiler
can "fall back" to discarding the register specification and treating it
like a normal stack variable should there be no register available.
I use the register keyword to tell the compiler that a variable is used a
lot eg as a loop index. What I "really want" is for the compiler to
optimize access to that variable - putting it in a register is only a
means to an end!
"register" is a relic of earlier times with less intelligent optimizing
compilers. The compiler can figure out for itself that a variable is
used heavily, especially a local variable (which is all you can apply
"register" to anyway).