typeof and g++

F

fabioppp

Hi there,
g++'s typeof is a very useful feature!
Will it never be in the Standard?
Is there any compliant way to implement it?
If I mean a function template returning a type, maybe is this
impossible?
What's about g++'s typeof implementation; how does it
work internally?

Thank.
 
J

John Smith

g++'s typeof is a very useful feature!

Could you please give an example where you use it?
I can't think of any scenario which can't be solved otherwise.

-- John
 
F

fabioppp

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.

-- John

A template function deduce a type, and then return some values. I have
to know the type of the returned value.
If I use some trick with sizeof I think I could avoid the use of typeof.
But in this situation is really difficult to use sizeof in some way.
 
I

Ioannis Vranos

fabioppp said:
A template function deduce a type, and then return some values. I have
to know the type of the returned value.
If I use some trick with sizeof I think I could avoid the use of typeof.
But in this situation is really difficult to use sizeof in some way.


For run-time type identification (RTTI) standard C++ provides
dynamic_cast. For extended type information it provides typeid.


Chapter 15 of TC++PL covers these. A snip from there:

"The dynamic_cast operator serves most needs for information about the
type of an object at run-time. Importantly, it ensures that code written
using it works correctly with classes derived from those explicitly
mentioned by the programmer. Thus, dynamic_cast preserves flexibility
and extensibility in a manner similar to virtual functions.

However, it is occasionally essential to know the exact type of an
object. For example, we might like to know the name of the object’s
class or its layout. The typeid operator serves this purpose by yielding
an object representing the type of its operand."
 
A

Arkadiy

fabioppp said:
Hi there,
g++'s typeof is a very useful feature!
Will it never be in the Standard?

It will, eventually. There is a proposal under discussion:
http://www.osl.iu.edu/~jajarvi/publications/papers/decltype_n1478.pdf
Is there any compliant way to implement it?
If I mean a function template returning a type, maybe is this
impossible?

It is possible. See BOOST_TYPEOF proposal (typeof.zip) in the boost
sandbox file vault:

http://boost-sandbox.sourceforge.net/vault/

Regards,
Arkadiy
 
P

Phillip Jordan

fabioppp said:
A template function deduce a type, and then return some values. I have
to know the type of the returned value.
If I use some trick with sizeof I think I could avoid the use of typeof.
But in this situation is really difficult to use sizeof in some way.

Please give a more detailed example. gcc's/g++'s typeof is entirely
compile-time, so surely standard C++ templates are able to provide your
desired functionality.

The only useful uses for typeof I know of are macros. It gives them
template-like powers, so that you're able to get something close to
generic programming in C. In C++, you shouldn't need it.

~phil
 
A

Arkadiy

I think the best example is when you need to alocate a named object of
some expression-template (using Boost.Lambda):

lambda_functor<
lambda_functor_base<
logical_action<and_action>,
tuple<
lambda_functor<
lambda_functor_base<
relational_action<greater_action>,
tuple<
lambda_functor said:
lambda_functor<
lambda_functor_base<
relational_action<less_action>,
tuple<
f = _1 > 15 && _2 < 20;

as opposed to just (using auto that's defined through typeof):

auto f = _1 > 15 && _2 < 20;

Of course, this is not necessary, you can use the one above. The
question, however, is which one do you prefer :)

Regards,
Arkadiy
 
F

fabioppp

Arkadiy said:
I think the best example is when you need to alocate a named object of
some expression-template (using Boost.Lambda):

lambda_functor<


f = _1 > 15 && _2 < 20;

as opposed to just (using auto that's defined through typeof):

auto f = _1 > 15 && _2 < 20;

I'm not using expression template.
It's about a compile time reflection. I have not a tree of expressions,
but a tree of types. It's the same.
It's a problem of time... I could need hours to write the right
[<...<...<..<blah, <blah>, blah>...>...>...> ], while with typeof I
could shut down my mind... and the compiler does the rest...
 
A

Arkadiy

fabioppp said:
I'm not using expression template.
It's about a compile time reflection. I have not a tree of expressions,
but a tree of types. It's the same.

In my example the result type of an expression is a tree of types
(templates). In modern C++ people use template parameter deduction
mechanism all the time to simplify creation of objects of complicated
types. As long as all that is needed is to pass this expression
somewhere, typeof is not necessary:

for_each(v.begin(), v.end(), cout << _1 << "\n");

Once you need to allocate a named object, though, it turnes out that
the actual type of the innocent-looking cout << _1 << "\n" functor is
similar to what I showed above, and you are much better off using
typeof/auto.

auto fun = cout << _1 << "\n";

Regards,
Arkadiy
 
B

Ben Hetland

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.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top