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

问题描述




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


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

推荐答案




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



本身?



未经测试的代码

模板< 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




实际上我认为上面是垃圾。道歉,我会离开并实际上测试一些东西。



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


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

08-24 02:13