type that pointer points to

L

Levent

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 :)
 
I

Ivan Vecerina

Levent said:
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
 
J

Jonathan Mcdougall

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
 
L

Levent

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

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

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


Jonathan
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top