473,508 Members | 2,412 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

type that pointer points to

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 :)
Jul 23 '05 #1
3 1404
"Levent" <sl**@pitt.edu> wrote in message
news:d5**********@usenet01.srv.cis.pitt.edu...
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
Jul 23 '05 #2
> 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

Jul 23 '05 #3
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

Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
2043
by: David Sobey | last post by:
Hi everyone I'm having huge difficulties overriding a virtual function with a function that returns a covariant type. I'd be grateful if someone could show me some code where this is done: - A...
25
10121
by: hugo2 | last post by:
Obrhy/hugo July 12, 2004 Take a look at this memcpy() definition. Is there a good reason the void pointer args are cast to byte just to assign their addresses to byte pointers? /*from Steve...
16
10349
by: Abhishek | last post by:
why do I see that in most C programs, pointers in functions are accepted as: int func(int i,(void *)p) where p is a pointer or an address which is passed from the place where it is called. what...
7
2844
by: Arpan | last post by:
The .NET Framework 2.0 documentation states that An Object variable always holds a pointer to the data, never the data itself. Now w.r.t. the following ASP.NET code snippet, can someone please...
13
11958
by: william | last post by:
code segment: long int * size; char entry; ............. size=&entry; ************************************* Gcc reported 'assignment of incompatible pointer type'.
13
336
by: atif | last post by:
Hi It is well known that Pointer just holds address. But how the machine came to know that the pointer is holding an address of an Integer so i will read just 4 bytes and in case of Long 8...
3
29672
by: David Mathog | last post by:
I have a program for which this line: if(! lstrtol(&atoken,length-2,(long *) &(lclparams->pad)) || (lclparams->pad< 0)){ generates the warning below, but ONLY if the gcc compiler is at -O2 or...
6
1888
by: Generic Usenet Account | last post by:
I ran a small experiment involving RTTI and dynamic casting. Basically what I did was to cast a base class pointer to a derived type (yes, I know that is not kosher). I then performed...
3
1817
by: somenath | last post by:
Hi All, I got some questions regarding the type of expression . For example 1)char *const *(*ptr)(); Here the type of ptr is it is constant pointer to a function which accept unspecified...
11
3654
by: Neo | last post by:
If i have a : typedef struct str32{ uint32_t word1; uint32_t word2; } word_array; word_array *my_array; what would be the data type of this: myarray->word1
0
7224
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7118
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7323
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7493
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5625
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5049
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1550
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.