Pulling the type declaration out of a variable?

Discussion in 'C++' started by Steven T. Hatton, May 22, 2004.

  1. Somewhere I seem to recall an example where the author was able to access
    some kind of representation of a variable's type and use it to create
    another variable of the same type. It may have been in the context of
    templates.

    Here's why I ask; I have a member field declared like this:

    protected:
    vector<vector<SbVec3f*> >* strips_ptr;

    Within a function, I came across a reason to want a local variable of the
    same type. The alternatives are to either use the same long (painful to
    type) declaration, or use a typedef. I personally dislike typedefs in most
    situations. They seem like just another level of indirection with the
    potential to obfuscate and confuse.

    OTOH, if I can create a variable in such a way that I can see it's another
    instance of one I'm already using, that seems more informative and direct.
    --
    STH
    Hatton's Law: "There is only One inviolable Law"
    KDevelop: http://www.kdevelop.org SuSE: http://www.suse.com
    Mozilla: http://www.mozilla.org
    Steven T. Hatton, May 22, 2004
    #1
    1. Advertising

  2. Steven T. Hatton wrote:
    > Somewhere I seem to recall an example where the author was able to access
    > some kind of representation of a variable's type and use it to create
    > another variable of the same type. It may have been in the context of
    > templates.
    >
    > Here's why I ask; I have a member field declared like this:
    >
    > protected:
    > vector<vector<SbVec3f*> >* strips_ptr;
    >
    > Within a function, I came across a reason to want a local variable of the
    > same type. The alternatives are to either use the same long (painful to
    > type) declaration, or use a typedef. I personally dislike typedefs in most
    > situations. They seem like just another level of indirection with the
    > potential to obfuscate and confuse.
    >
    > OTOH, if I can create a variable in such a way that I can see it's another
    > instance of one I'm already using, that seems more informative and direct.



    The way to extract a type using standard C++ is by using template
    functions. However, gcc does have an extension called "typeof" which
    allows you to do type extraction without resorting to complex templates.
    typeof is not supported universally.

    If you can place your algorithm that needs a local variable of your
    particular type in a template function then this is pretty easy.

    template <typename T> void func( T input )
    {
    T local_variable;
    }

    calling "func( strips_ptr )" gives you that local variable.
    Gianni Mariani, May 22, 2004
    #2
    1. Advertising

  3. "Steven T. Hatton" <> wrote in message
    news:...
    | Somewhere I seem to recall an example where the author was able to access
    | some kind of representation of a variable's type and use it to create
    | another variable of the same type. It may have been in the context of
    | templates.
    |
    | Here's why I ask; I have a member field declared like this:
    |
    | protected:
    | vector<vector<SbVec3f*> >* strips_ptr;
    |
    | Within a function, I came across a reason to want a local variable of the
    | same type. The alternatives are to either use the same long (painful to
    | type) declaration, or use a typedef. I personally dislike typedefs in most
    | situations. They seem like just another level of indirection with the
    | potential to obfuscate and confuse.
    |
    | OTOH, if I can create a variable in such a way that I can see it's another
    | instance of one I'm already using, that seems more informative and direct.

    I'm not sure that I understand exactly what it is
    your asking, however, I have to disagree with you,
    that typedef' can obfuscate and confuse - I think
    that they are a good thing, and do exactly what they
    were intended to do quite nicely.

    Cheers.
    Chris Val
    Chris \( Val \), May 22, 2004
    #3
  4. Steven T. Hatton

    JKop Guest

    Steven T. Hatton posted:

    > Within a function, I came across a reason to want a local variable of
    > the same type. The alternatives are to either use the same long
    > (painful to type) declaration, or use a typedef. I personally dislike
    > typedefs in most situations. They seem like just another level of
    > indirection with the potential to obfuscate and confuse.



    typedef
    MammalWithFourLegsThatBarksAndWaggsItsTailWhenItsHappyAndLowersItsEarsWhenIt
    sSadOrAfraid Dog;


    I don't see the problem.


    -JKop
    JKop, May 22, 2004
    #4
    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. Daniel Jahro via DotNetMonster.com

    Pulling out 10 last posts in forum (access db)

    Daniel Jahro via DotNetMonster.com, Feb 3, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    393
    Patrick Olurotimi Ige
    Feb 4, 2005
  2. C.Thomson

    Pulling out duplicates

    C.Thomson, Nov 29, 2004, in forum: Java
    Replies:
    2
    Views:
    361
    xarax
    Nov 29, 2004
  3. Replies:
    3
    Views:
    495
    Wibble
    May 28, 2005
  4. Bolin
    Replies:
    4
    Views:
    402
  5. Luca Forlizzi
    Replies:
    4
    Views:
    448
    Luca Forlizzi
    Nov 14, 2010
Loading...

Share This Page