Connecting Tech Pros Worldwide Forums | Help | Site Map

Finding the type of a pointed to element

Adam H. Peterson
Guest
 
Posts: n/a
#1: Jul 22 '05
I have a type T (in a template) which I know (or assume) is a pointer to
a pair type. I would like to find out what the type of the second
element in the pair is. If T were simply a pair, I could get this by
doing something like:

typedef typename T::second_type U;

But how do I handle when T is a _pointer_to_ a pair type (or actually an
iterator type for a pointer to some pair type)?

Thanks in advance,
Adam Peterson

David Harmon
Guest
 
Posts: n/a
#2: Jul 22 '05

re: Finding the type of a pointed to element


On Tue, 06 Apr 2004 15:53:02 -0600 in comp.lang.c++, "Adam H. Peterson"
<ahp6@email.byu.edu> wrote,[color=blue]
>I have a type T (in a template) which I know (or assume) is a pointer to
>a pair type. I would like to find out what the type of the second
>element in the pair is. If T were simply a pair, I could get this by
>doing something like:
>
>typedef typename T::second_type U;[/color]

If your compiler has good support for template partial specialization I
think you should be able to use

typedef typename std::iterator_traits<T>::value_type::second_type U;

David Harmon
Guest
 
Posts: n/a
#3: Jul 22 '05

re: Finding the type of a pointed to element


On Tue, 06 Apr 2004 15:53:02 -0600 in comp.lang.c++, "Adam H. Peterson"
<ahp6@email.byu.edu> wrote,[color=blue]
>I have a type T (in a template) which I know (or assume) is a pointer to
>a pair type. I would like to find out what the type of the second
>element in the pair is. If T were simply a pair, I could get this by
>doing something like:
>
>typedef typename T::second_type U;[/color]

If your compiler has good support for template partial specialization I
think you should be able to use

typedef typename std::iterator_traits<T>::value_type::second_type U;

Adam H. Peterson
Guest
 
Posts: n/a
#4: Jul 22 '05

re: Finding the type of a pointed to element


David Harmon wrote:[color=blue]
> On Tue, 06 Apr 2004 15:53:02 -0600 in comp.lang.c++, "Adam H. Peterson"
> <ahp6@email.byu.edu> wrote,
>[color=green]
>>I have a type T (in a template) which I know (or assume) is a pointer to
>>a pair type. I would like to find out what the type of the second
>>element in the pair is. If T were simply a pair, I could get this by
>>doing something like:
>>
>>typedef typename T::second_type U;[/color]
>
>
> If your compiler has good support for template partial specialization I
> think you should be able to use
>
> typedef typename std::iterator_traits<T>::value_type::second_type U;
>[/color]

g++ 3.2.2 20030222

Bravo. Thanks, it appears to work.

Adam
Adam H. Peterson
Guest
 
Posts: n/a
#5: Jul 22 '05

re: Finding the type of a pointed to element


David Harmon wrote:[color=blue]
> On Tue, 06 Apr 2004 15:53:02 -0600 in comp.lang.c++, "Adam H. Peterson"
> <ahp6@email.byu.edu> wrote,
>[color=green]
>>I have a type T (in a template) which I know (or assume) is a pointer to
>>a pair type. I would like to find out what the type of the second
>>element in the pair is. If T were simply a pair, I could get this by
>>doing something like:
>>
>>typedef typename T::second_type U;[/color]
>
>
> If your compiler has good support for template partial specialization I
> think you should be able to use
>
> typedef typename std::iterator_traits<T>::value_type::second_type U;
>[/color]

g++ 3.2.2 20030222

Bravo. Thanks, it appears to work.

Adam
Closed Thread


Similar C / C++ bytes