Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > typeof and g++

Reply
Thread Tools

typeof and g++

 
 
fabioppp
Guest
Posts: n/a
 
      02-23-2005
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.

--
fabioppp
 
Reply With Quote
 
 
 
 
John Smith
Guest
Posts: n/a
 
      02-23-2005
> 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


 
Reply With Quote
 
 
 
 
fabioppp
Guest
Posts: n/a
 
      02-23-2005
John Smith wrote:
>>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
>
>


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.

--
Fabio
 
Reply With Quote
 
Ioannis Vranos
Guest
Posts: n/a
 
      02-23-2005
fabioppp wrote:

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



--
Ioannis Vranos

http://www23.brinkster.com/noicys
 
Reply With Quote
 
Arkadiy
Guest
Posts: n/a
 
      02-23-2005
fabioppp wrote:
> 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/publi...type_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

 
Reply With Quote
 
Phillip Jordan
Guest
Posts: n/a
 
      02-23-2005
fabioppp wrote:
> 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
 
Reply With Quote
 
Arkadiy
Guest
Posts: n/a
 
      02-23-2005
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<placeholder<1> >,
int const
>
>
>,

lambda_functor<
lambda_functor_base<
relational_action<less_action>,
tuple<
lambda_functor<placeholder<2> >,
int const
>
>
>
>
>
>

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

 
Reply With Quote
 
fabioppp
Guest
Posts: n/a
 
      02-23-2005
Arkadiy wrote:
> I think the best example is when you need to alocate a named object of
> some expression-template (using Boost.Lambda):
>
> lambda_functor<
> [<...<...<..<blah, <blah>, blah>...>...>...> ]
> >

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

--
fabioppp
 
Reply With Quote
 
Arkadiy
Guest
Posts: n/a
 
      02-23-2005
fabioppp wrote:

> 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

 
Reply With Quote
 
Jonathan Turkanis
Guest
Posts: n/a
 
      02-23-2005
Arkadiy wrote:
> fabioppp wrote:
>> 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/publi...type_n1478.pdf


This is a pretty old reference. A newer one is here:

http://www.open-std.org/jtc1/sc22/wg...2004/n1705.pdf

See also

http://www.open-std.org/jtc1/sc22/wg...2004/n1721.pdf

Jonathan


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Get List of Roles and Translate(typeof(NTAccount)) Mario.Trafficante@qg.com ASP .Net Security 1 08-09-2006 02:00 PM
typeof emulation Arkadiy C++ 0 02-24-2005 09:28 PM
Using TypeOf? Leon ASP .Net 7 11-01-2004 11:41 PM
Use of typeof() for member data declaration (in g++) M. Steinbach C++ 1 02-20-2004 10:28 AM
Use of typeof() for member data declaration (g++ bug?) M. Steinbach C++ 0 02-20-2004 08:40 AM



Advertisments