473,324 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

How to convert (type *) to (type) inside a template?

Hi,

How to convert a type inside a template from pointer to type to type itself?

struct S { };

template<class It> void f(It begin, It end)
{
// When called from g(), It::value_type is a pointer to S, but
// I want to get S itself, for example to use it in an auto_ptr:
auto_ptr<It::*value_type> ap = **begin; // this will not work
}

void g()
{
std::vector<S *> v;
f(v.begin(), v.end());
}

There's a way to make pointer type from regular type (just by adding *), but
is there a way to do the reverse?

Best regards,
Marcin
Jul 22 '05 #1
11 1847

"Marcin Kalicinski" <ka****@poczta.onet.pl> wrote in message
news:c0**********@korweta.task.gda.pl...
Hi,

How to convert a type inside a template from pointer to type to type itself?
struct S { };

template<class It> void f(It begin, It end)
{
// When called from g(), It::value_type is a pointer to S, but
// I want to get S itself, for example to use it in an auto_ptr:
auto_ptr<It::*value_type> ap = **begin; // this will not work
}

void g()
{
std::vector<S *> v;
f(v.begin(), v.end());
}

There's a way to make pointer type from regular type (just by adding *), but is there a way to do the reverse?

Best regards,
Marcin


Partial template specialization will do the trick (but not all compilers
support it)

Untested code

template <class T>
struct UnPtr
{
};

template <class T>
struct UnPtr<T*>
{
typedef T type;
};

template<class It> void f(It begin, It end)
{
// When called from g(), It::value_type is a pointer to S, but
// I want to get S itself, for example to use it in an auto_ptr:
auto_ptr<UnPtr<It::value_type>::type > ap = **begin; // this will not
work
}

john
Jul 22 '05 #2

"John Harrison" <jo*************@hotmail.com> wrote in message
news:c0*************@ID-196037.news.uni-berlin.de...

"Marcin Kalicinski" <ka****@poczta.onet.pl> wrote in message
news:c0**********@korweta.task.gda.pl...
Hi,

How to convert a type inside a template from pointer to type to type

itself?

struct S { };

template<class It> void f(It begin, It end)
{
// When called from g(), It::value_type is a pointer to S, but
// I want to get S itself, for example to use it in an auto_ptr:
auto_ptr<It::*value_type> ap = **begin; // this will not work
}

void g()
{
std::vector<S *> v;
f(v.begin(), v.end());
}

There's a way to make pointer type from regular type (just by adding *),

but
is there a way to do the reverse?

Best regards,
Marcin


Partial template specialization will do the trick (but not all compilers
support it)

Untested code

template <class T>
struct UnPtr
{
};

template <class T>
struct UnPtr<T*>
{
typedef T type;
};


Actually I think that above is rubbish. Apologies, I'll go away and actually
test something.

john
Jul 22 '05 #3
On Thu, 12 Feb 2004 09:41:04 -0000, "John Harrison" <jo*************@hotmail.com> wrote:
Partial template specialization will do the trick (but not all compilers
support it)

Untested code

template <class T>
struct UnPtr
{
};

template <class T>
struct UnPtr<T*>
{
typedef T type;
};


Actually I think that above is rubbish. Apologies, I'll go away and actually
test something.


Uh, why?

I had to look in Andrei's book, but he uses the same technique.

Here's my test code:
#include <memory> // std::auto_ptr
#include <iostream> // std::cout
#include <typeinfo>

template< typename P >
struct PointerTraits {};

template< typename ABaseType >
struct PointerTraits<ABaseType*>
{
typedef ABaseType BaseType;
};

template< typename Pointer >
void f( Pointer p )
{
typedef PointerTraits<Pointer>::BaseType BaseType;
std::cout << typeid( Pointer ).name() << std::endl;
std::cout << typeid( BaseType ).name() << std::endl;
}

int main(){ int x; f( &x ); }

Jul 22 '05 #4
Marcin Kalicinski wrote:
Hi,

How to convert a type inside a template from pointer to type to type itself?

struct S { };

template<class It> void f(It begin, It end)
{
// When called from g(), It::value_type is a pointer to S, but
// I want to get S itself, for example to use it in an auto_ptr:
auto_ptr<It::*value_type> ap = **begin; // this will not work ^
|
+-- You cannot place '*' here ! }

void g()
{
std::vector<S *> v;
f(v.begin(), v.end());
}

There's a way to make pointer type from regular type (just by adding *), but
is there a way to do the reverse?

Best regards,
Marcin


I think that you will need to pass one more template parameter to the
function f since there is currently no possible way to deduce the S type
from It::value_type.

Furthermore, you can't dereference a pointer type by placing a start
ahead (see your annotated code sample) like dereferencing a pointer
(that is an address to an object).

Example:

typedef int* A;
typedef *Ptr B; // Wrong

--
Guillaume Brocker
Jul 22 '05 #5
* Guillaume Brocker <gu***************@ircad.u-strasbg.fr> wrote:
// When called from g(), It::value_type is a pointer to S, but
// I want to get S itself, for example to use it in an auto_ptr:


I think that you will need to pass one more template parameter to the
function f since there is currently no possible way to deduce the S type
from It::value_type.


That turns out not to be the case.

In fact S can easily be deduced from S*.

See the rest of the thread.

Jul 22 '05 #6
Hi All,

Thanks for the partial template specialization solution. Unfortunately, I
use Visual C++ 7.0 (not 7.1), which does not support it. I could not even
test if it works :-(

Is there any technique that does not rely on partial template
specialization?

Kind regards,
Marcin
Użytkownik "Marcin Kalicinski" <ka****@poczta.onet.pl> napisał w wiadomości
news:c0**********@korweta.task.gda.pl...
Hi,

How to convert a type inside a template from pointer to type to type itself?

struct S { };

template<class It> void f(It begin, It end)
{
// When called from g(), It::value_type is a pointer to S, but
// I want to get S itself, for example to use it in an auto_ptr:
auto_ptr<It::*value_type> ap = **begin; // this will not work
}

void g()
{
std::vector<S *> v;
f(v.begin(), v.end());
}

There's a way to make pointer type from regular type (just by adding *), but
is there a way to do the reverse?

Best regards,
Marcin

Jul 22 '05 #7
Alf P. Steinbach wrote:
* Guillaume Brocker <gu***************@ircad.u-strasbg.fr> wrote:

// When called from g(), It::value_type is a pointer to S, but
// I want to get S itself, for example to use it in an auto_ptr:


I think that you will need to pass one more template parameter to the
function f since there is currently no possible way to deduce the S type
from It::value_type.

That turns out not to be the case.

In fact S can easily be deduced from S*.

See the rest of the thread.


You are right...
Jul 22 '05 #8
* "Marcin Kalicinski" <ka****@poczta.onet.pl> wrote:
Thanks for the partial template specialization solution. Unfortunately, I
use Visual C++ 7.0 (not 7.1), which does not support it. I could not even
test if it works :-(


Please do not top-post.

Regarding partial template specialization you can often simulate it by using
nested template classes or functions.

However, I suspect that's not possible in this case, mainly because the VC 7.0
port of Loki TypeTraits does not implement this functionality. If it were
possible I think it would be there. So, practical solution, more arguments.

Jul 22 '05 #9

"Alf P. Steinbach" <al***@start.no> wrote in message
news:40***************@News.CIS.DFN.DE...
On Thu, 12 Feb 2004 09:41:04 -0000, "John Harrison" <jo*************@hotmail.com> wrote:
Partial template specialization will do the trick (but not all compilers support it)

Untested code

template <class T>
struct UnPtr
{
};

template <class T>
struct UnPtr<T*>
{
typedef T type;
};


Actually I think that above is rubbish. Apologies, I'll go away and actuallytest something.


Uh, why?

I had to look in Andrei's book, but he uses the same technique.


I got worried because I hadn't tested the code. I convinced myself that the
T in 'typedef T type' was the pointer type not the type pointed to. Seems I
was wrong so I apologise for my apology!

john
Jul 22 '05 #10
Alf P. Steinbach wrote:
On Thu, 12 Feb 2004 09:41:04 -0000, "John Harrison" <jo*************@hotmail.com> wrote:

Partial template specialization will do the trick (but not all compilers
support it)

Untested code

template <class T>
struct UnPtr
{
};

template <class T>
struct UnPtr<T*>
{
typedef T type;
};

Actually I think that above is rubbish. Apologies, I'll go away and actually
test something.

Uh, why?

I had to look in Andrei's book, but he uses the same technique.

Here's my test code:
#include <memory> // std::auto_ptr
#include <iostream> // std::cout
#include <typeinfo>

template< typename P >
struct PointerTraits {};

template< typename ABaseType >
struct PointerTraits<ABaseType*>
{
typedef ABaseType BaseType;
};

template< typename Pointer >
void f( Pointer p )
{
typedef


typename
PointerTraits<Pointer>::BaseType BaseType;
std::cout << typeid( Pointer ).name() << std::endl;
std::cout << typeid( BaseType ).name() << std::endl;
}

int main(){ int x; f( &x ); }

I'm still not clear on when "typename" is(not) needed, but GCC insists
on it in this case.

Jul 22 '05 #11
"John Harrison" <jo*************@hotmail.com> wrote in message news:<c0*************@ID-196037.news.uni-berlin.de>...
"Marcin Kalicinski" <ka****@poczta.onet.pl> wrote in message
news:c0**********@korweta.task.gda.pl...
Hi,

How to convert a type inside a template from pointer to type to type

itself?
[snip]


Partial template specialization will do the trick (but not all compilers
support it)

Untested code

template <class T>
struct UnPtr
{
};

template <class T>
struct UnPtr<T*>
{
typedef T type;
};

template<class It> void f(It begin, It end)
{
// When called from g(), It::value_type is a pointer to S, but
// I want to get S itself, for example to use it in an auto_ptr:
auto_ptr<UnPtr<It::value_type>::type > ap = **begin; // this will not
work
}


What would UnPtr<int>::type give you then? Or UnPtr<int***>::type?
Perhaps the following slight modification would be more flexible...

template<class T>
struct UnPtr
{
typedef T type;
};

template<class T>
struct UnPtr<T*>
{
typedef typename UnPtr<T>::type type;
};

void f()
{
UnPtr<int>::type i = 1;
UnPtr<int****>::type j = 1;
}
Jul 22 '05 #12

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

Similar topics

3
by: david.corby | last post by:
Hi again everyone! I was wondering if there is any way to specify a return type for a template class member function that is typedef'd in the class. I.E... template <class T> class A { ...
5
by: Dave | last post by:
The following code won't compile for me, it does not recognize my typedef'd type as a type: template <class T> class A { public: typedef int TD; private: TD b (); };
3
by: Dan | last post by:
Good Day All, I have the following VB6 Type Type IAListRecordType lID As Long strName As String * IALISTSTRSIZE End Type I need to convert this to VB .NET. How do I do this? I have come...
2
by: Joseph Lu | last post by:
Hi, I have a multithread problem like the following lines, when I compile this code I caught a "error C2665", the error description is : none of the number1 overloads can convert parameter number2...
7
by: quarup | last post by:
I want to specialize a template function that lives inside a class, but am getting a compile error in VS.net 2003. Here's my code: template <class T> class A { public: template <class U> void...
1
by: shuisheng | last post by:
Dear All, I have template classes as following template<class T> struct A { typedef T DataType; DataType valueA; };
1
by: neeraj | last post by:
Hi All Can any give me the code for convert "DataColumn" data type of "DataTable". Even if data table already populated (have data) Actually I am creating one search module which searches the...
10
by: Yevgen Muntyan | last post by:
Consider the following macro: #define ALLOCIT(Type) ((Type*) malloc (sizeof (Type))) The intent is to wrap raw memory allocation of N bytes into a macro which returns allocated chunk of memory...
3
by: skt05001 | last post by:
Hi, I am creating schema programatically and I would need a method to convert .NET type to XSD type so that I could pass on the type string to XMLQualifiedName class. For example, I would need...
7
by: Bill Davy | last post by:
I want to be able to write (const char*)v where v is an item of type Class::ToolTypeT where ToolTypeT is an enumeration and I've tried everything that looks sensible. There's an ugly solution, but...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shćllîpôpď 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.