Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > type that pointer points to

Reply
Thread Tools

type that pointer points to

 
 
Levent
Guest
Posts: n/a
 
      05-05-2005
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
 
Reply With Quote
 
 
 
 
Ivan Vecerina
Guest
Posts: n/a
 
      05-05-2005
"Levent" <(E-Mail Removed)> wrote in message
news:d5e29r$iau$(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
 
Jonathan Mcdougall
Guest
Posts: n/a
 
      05-05-2005
> 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

 
Reply With Quote
 
Levent
Guest
Posts: n/a
 
      05-05-2005
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
>
>
>

 
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
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
evaluate NULL to a pointer which a CONST pointer points to G C++ 3 01-08-2007 04:22 PM
passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer jimjim C Programming 16 03-27-2006 11:03 PM
Why (type*)pointer isn't equal to *(type**)pointer? lovecreatesbeauty C Programming 10 01-17-2006 09:30 AM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM



Advertisments