typeof and g++

Discussion in 'C++' started by fabioppp, Feb 23, 2005.

  1. fabioppp

    fabioppp Guest

    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
    fabioppp, Feb 23, 2005
    #1
    1. Advertising

  2. fabioppp

    John Smith Guest

    > 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
    John Smith, Feb 23, 2005
    #2
    1. Advertising

  3. fabioppp

    fabioppp Guest

    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
    fabioppp, Feb 23, 2005
    #3
  4. 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
    Ioannis Vranos, Feb 23, 2005
    #4
  5. fabioppp

    Arkadiy Guest

    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/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
    Arkadiy, Feb 23, 2005
    #5
  6. 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
    Phillip Jordan, Feb 23, 2005
    #6
  7. fabioppp

    Arkadiy Guest

    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
    Arkadiy, Feb 23, 2005
    #7
  8. fabioppp

    fabioppp Guest

    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
    fabioppp, Feb 23, 2005
    #8
  9. fabioppp

    Arkadiy Guest

    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
    Arkadiy, Feb 23, 2005
    #9
  10. Jonathan Turkanis, Feb 23, 2005
    #10
  11. fabioppp

    Ben Hetland Guest

    John Smith wrote:

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

    --
    Regards,
    -+-Ben Hetland-+-
    Ben Hetland, Feb 24, 2005
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Leon

    Using TypeOf?

    Leon, Nov 1, 2004, in forum: ASP .Net
    Replies:
    7
    Views:
    2,141
    Henri
    Nov 1, 2004
  2. M. Steinbach
    Replies:
    1
    Views:
    620
    tom_usenet
    Feb 20, 2004
  3. M. Steinbach
    Replies:
    0
    Views:
    336
    M. Steinbach
    Feb 20, 2004
  4. Arkadiy

    typeof emulation

    Arkadiy, Feb 24, 2005, in forum: C++
    Replies:
    0
    Views:
    746
    Arkadiy
    Feb 24, 2005
  5. Replies:
    1
    Views:
    408
    Joe Kaplan \(MVP - ADSI\)
    Aug 9, 2006
Loading...

Share This Page