John said:
Could you please give an example where you use it?
I can't think of any scenario which can't be solved otherwise.
I could give a real-life practical example (unrelated to g++) where a
typeof would be a very useful feature.
A couple of weeks back I was "porting" some Windows code using MFC from
the VC++6.0 compiler to the 7.1 compiler. The point here is that some of
the types used have changed in a rather significant way between the two
versions of MFC being used. In particular file positions and time values
have changed from 32-bit to 64-bit entities. Of course, as typical in
MFC, such stuff doesn't have its own type declaration, but rather tends
to rely on "size-specific" typedefs.
The code goes somewhat along the lines of:
DWORD pos = file.GetPosition(); // CFile file;
// ...some more reading from the file
file.Seek( pos, CFile::begin );
Except this doesn't work with large files as supported in the updated
MFC version... (Luckily the compiler warns!) Here the return type of
GetPosition and the corresponding argument type to Seek has changed to
ULONGLONG instead, and I don't want to change all such declarations,
just in order to find out that I have to change them again on the next
update!
In this case I think the most elegant solution would be to be able to
write simply
typeof(file.GetPosition()) pos = file.GetPosition();
// ...
file.Seek( pos, CFile::begin );
The second-best solution is to use my own typedef, with a conditional
compile to define that one. As you say, it _can_ be solved otherwise,
but the typeof alternative would be much more maintainable.