如何在模板中转换(类型*)到(类型)? [英] How to convert (type *) to (type) inside a template?

查看:68
本文介绍了如何在模板中转换(类型*)到(类型)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




如何将模板内的类型从指针转换为类型到类型本身?


struct S {};


模板< class It> void f(开始,结束)

{

//从g()调用时,它:: value_type是指向S的指针,但是

//我想得到S本身,例如在auto_ptr中使用它:

auto_ptr< It :: * value_type> ap = **开始; //这不起作用

}


void g()

{

std: :vector< S *> v;

f(v.begin(),v.end());

}


有一种方法从常规类型制作指针类型(只需添加*),但是

有没有办法反过来?


祝你好运,

Marcin

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

推荐答案



" Marcin Kalicinski" < KA **** @ poczta.onet.pl>在消息中写道

news:c0 ********** @ korweta.task.gda.pl ...

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

<如何将模板内的类型从指针转换为类型以键入
本身?
struct S {};

template< class It> void f(它开始,结束)
//
//从g()调用时,它:: value_type是指向S的指针,但是
//我想得到S本身,例如在auto_ptr中使用它:
auto_ptr< It :: * value_type> ap = **开始; //这不起作用
}

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

有一种方法可以从常规类型中创建指针类型(只需添加*),
但有没有办法做相反的事情?

最好的问候,
Marcin
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模板专业化将起作用(但不是所有编译器

支持它)


未经测试的代码


模板< ; T类>

struct UnPtr

{

};


模板< class T> ;

struct UnPtr< T *>

{

typedef T type;

};


模板< class It> void f(开始,结束)

{

//从g()调用时,它:: value_type是指向S的指针,但是

//我想获得S本身,例如在auto_ptr中使用它:

auto_ptr< UnPtr< It :: value_type> :: type> ap = **开始; //这不会

工作

}


john



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




" John Harrison" <乔************* @ hotmail.com>在留言中写道

news:c0 ************* @ ID-196037.news.uni-berlin.de ...

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

Marcin Kalicinski < KA **** @ poczta.onet.pl>在消息中写道
新闻:c0 ********** @ korweta.task.gda.pl ...

"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


本身?


struct S {};

模板< class It> void f(它开始,结束)
//
//从g()调用时,它:: value_type是指向S的指针,但是
//我想得到S本身,例如在auto_ptr中使用它:
auto_ptr< It :: * value_type> ap = **开始; //这不起作用
}

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

有一种方法可以从常规类型中创建指针类型(只需添加*),

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 *),


有没有办法做相反的事情?

致以最诚挚的问候,
Marcin
is there a way to do the reverse?

Best regards,
Marcin

<部分模板专业化将起作用(但并非所有编译器都支持它)

未经测试的代码

模板< class T>
struct UnPtr
{
};

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



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;
};




实际上我认为上面是垃圾。道歉,我会离开,实际上是

测试一下。


john



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

john


On星期四,2004年2月12日09:41:04 -0000,约翰哈里森 <乔************* @ hotmail.com>写道:
On Thu, 12 Feb 2004 09:41:04 -0000, "John Harrison" <jo*************@hotmail.com> wrote:
部分模板专业化将起作用(但并非所有编译器都支持它)

未经测试代码

模板< class T>
struct UnPtr
{
};

模板< class T>
struct UnPtr< T *>
{
typedef T type;
};
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.




呃,为什么?


我我不得不看安德烈的书,但他使用相同的技术。


这是我的测试代码:

#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;

};


模板< typename指针>

void f(指针p)

{

typedef PointerTraits< Pointer> :: BaseType BaseType;

std :: cout<< typeid(指针).name()<< std :: endl;

std :: cout<< typeid(BaseType).name()<< std :: endl;

}


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



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 ); }


这篇关于如何在模板中转换(类型*)到(类型)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆