type that pointer points to

Discussion in 'C++' started by Levent, May 5, 2005.

  1. Levent

    Levent Guest

    Hi,

    Is there a generic way to dereference a pointer (of any order) enough
    to get the underlying type that the whole thing points to?

    Consider,

    template <class P>
    class Foo {
    typename THIS_IS_THE_WAY_TO_DO_IT(P) member;
    };

    such that, say, for the following instantiation of the template class Foo,

    Foo<int******> f;

    f.member will be an int.

    FWIW, I currently achieve this with specialization:

    template <class P>
    class Foo<P*> {
    P member;
    };

    template <class P>
    class Foo<P**> {
    P member;
    };

    template <class P>
    class Foo<P***> {
    P member;
    };

    an so forth, which is not cool :)
     
    Levent, May 5, 2005
    #1
    1. Advertising

  2. "Levent" <> wrote in message
    news:d5e29r$iau$...
    > Is there a generic way to dereference a pointer (of any order) enough to
    > get the underlying type that the whole thing points to?
    >
    > Consider,
    >
    > template <class P>
    > class Foo {
    > typename THIS_IS_THE_WAY_TO_DO_IT(P) member;
    > };
    >
    > such that, say, for the following instantiation of the template class Foo,
    >
    > Foo<int******> f;
    >
    > f.member will be an int.


    The following shall do it:


    template<class T>
    struct FullDeref
    { typedef T Type; };

    template<class T>
    struct FullDeref<T*>
    { typedef typename FullDeref<T>::Type Type; };


    template <class P>
    class Foo {
    typename FullDeref<P>::Type member;
    };

    // other usage example:
    void foo()
    {
    FullDeref<int*****>::Type i = 5;
    }



    Ivan
    --
    http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
    Brainbench MVP for C++ <> http://www.brainbench.com
     
    Ivan Vecerina, May 5, 2005
    #2
    1. Advertising

  3. > Is there a generic way to dereference a pointer (of any order)
    enough
    > to get the underlying type that the whole thing points to?


    template <class P>
    struct Foo
    {
    typedef P type;
    };

    template <class P>
    struct Foo<P*>
    {
    typedef typename Foo<P>::type type;
    };


    Jonathan
     
    Jonathan Mcdougall, May 5, 2005
    #3
  4. Levent

    Levent Guest

    this is what I call cool!
    thx Ivan& Jonathan...

    Jonathan Mcdougall wrote:

    >>Is there a generic way to dereference a pointer (of any order)
    >>
    >>

    >enough
    >
    >
    >>to get the underlying type that the whole thing points to?
    >>
    >>

    >
    >template <class P>
    >struct Foo
    >{
    > typedef P type;
    >};
    >
    >template <class P>
    >struct Foo<P*>
    >{
    > typedef typename Foo<P>::type type;
    >};
    >
    >
    >Jonathan
    >
    >
    >
     
    Levent, May 5, 2005
    #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. James.D
    Replies:
    6
    Views:
    411
    James.D
    Mar 5, 2004
  2. R.kumaran
    Replies:
    6
    Views:
    404
    Kevin Easton
    Sep 3, 2003
  3. Andrew

    Test if pointer points to allocated memory

    Andrew, Jan 21, 2004, in forum: C Programming
    Replies:
    34
    Views:
    949
    Daniel Haude
    Jan 26, 2004
  4. lovecreatesbeauty

    Why (type*)pointer isn't equal to *(type**)pointer?

    lovecreatesbeauty, Jan 14, 2006, in forum: C Programming
    Replies:
    10
    Views:
    502
    Peter Shaggy Haywood
    Jan 17, 2006
  5. G
    Replies:
    3
    Views:
    449
    Sylvester Hesp
    Jan 8, 2007
Loading...

Share This Page