void **到int **所需的强制转换 [英] cast required for void** to int**

查看:97
本文介绍了void **到int **所需的强制转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我在T上有一个模板数组类,当T

为void *时,它具有膨胀特性。因此,当T真的是一个int *时,我的基础ptr实际上是一个

void **并且需要转换为int **。


这个不起作用:


void ** base = ...;

int ** ibase = static_cast< int **>(base) ; //非法


这是不好的做法:


void ** base = ...;

int ** ibase = reinterpret_cast< int **>(base);


我该怎么办?我已被警告远离所有使用

reinterpret_cast的主持组,这似乎是一个新手

问题。


Andy

Hi,

I have a template array class on T with specialization for bloat when T
is void*. Thus when T is really an int*, my base ptr is physically a
void** and needs to be converted to an int**.

This doesn''t work:

void** base = ...;
int** ibase = static_cast<int**>(base); // illegal

and this is bad practice:

void** base = ...;
int** ibase = reinterpret_cast<int**>(base);

What am I supposed to do? I''ve been warned away from all use of
reinterpret_cast on the moderated group, and this seems to be a newbie
question.

Andy

推荐答案

一个********* @ yahoo.com 写道:

我在T上有一个模板数组类,专门用于当

T无效*时膨胀。因此当T真的是一个int *时,我的基础ptr实际上是一个void **并且需要转换为int **。
I have a template array class on T with specialization for bloat when
T is void*. Thus when T is really an int*, my base ptr is physically
a void** and needs to be converted to an int**.



需要?


让我这样......


模板< class Tclass myarray;


模板< class myarray< void * {

//一些实现

void * * m_storage;

public:

void * operator [](int i){return m_storage [i]; }

};


模板< class Tclass myarray< T *:public myarray< void * {

// what'在这里?

public:

T * operator [](int i){return static_cast< T *>(m_storage [i]); }

};


这就是你在做什么的?没问题,对吧?

"Needs"?

Let me get this...

template<class Tclass myarray;

template<class myarray<void*{
// some implementation
void** m_storage;
public:
void* operator[](int i) { return m_storage[i]; }
};

template<class Tclass myarray<T*: public myarray<void*{
// what''s here?
public:
T* operator[](int i) { return static_cast<T*>(m_storage[i]); }
};

Is that what you''re doing? No problem yet, right?


>

这不行:


void ** base = ...;

int ** ibase = static_cast< int **>(base); //非法
>
This doesn''t work:

void** base = ...;
int** ibase = static_cast<int**>(base); // illegal



为什么要尝试将void **转换为int **?

Why are you trying to convert void** to int**?


和这是不好的做法:


void ** base = ...;

int ** ibase = reinterpret_cast< int **>(base);


我该怎么办?我已被警告远离所有使用

reinterpret_cast的主持组,这似乎是一个新手

问题。
and this is bad practice:

void** base = ...;
int** ibase = reinterpret_cast<int**>(base);

What am I supposed to do? I''ve been warned away from all use of
reinterpret_cast on the moderated group, and this seems to be a newbie
question.



也许你选择了一个糟糕的算法。这是一个死胡同,你只是

不想承认给自己?


发布更多代码。


V

-

请在通过电子邮件回复时删除资金''A'

我请不要回复最热门的回复,请不要在消息中询问

Perhaps you''ve chosen a bad algorithm. It''s a dead end, and you just
don''t want to admit it to yourself?

Post more code.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


< an ********* @ yahoo.comwrote />
新闻:11 ********************* @ i3g2000cwc.googlegrou ps.com ...

:我在T上有一个模板数组类,专门用于膨胀时

T

:无效*。因此当T真的是一个int *时,我的基础ptr实际上是一个

:void **并且需要转换为int **。



:这不起作用:



:void ** base = ...;

:int ** ibase = static_cast< int **>(base); //非法



:这是不好的做法:



:void ** base = ......;

:int ** ibase = reinterpret_cast< int **>(基础);



:我是什么应该这么做?


从理论上讲,void *可能有不同的内存表示形式

而不是int * - 即使类型之间的转换是好吧

定义:

int i = 5;

void * p =& i;

*( int *)p = 6; //好的,我现在6

**(int **)& p = 7; //未定义的行为!!


因此,如果你想要严格的可移植代码,你需要确保

与void *数组的常见实现总是

作为void *数组发送。

:我已经被警告远离所有使用

:reinterpret_cast的主持组,以及这似乎是一个新手

:问题。


专门使用常用实现的模板不是很多一个新手问题。它曾经是几年前更常见的


请注意,今天的一些编译器/链接器会自动''合并''

相同的功能,并自动消除你想要避免的代码膨胀。

hth -Ivan

-
http://ivan.vecerina.com/contact/?subject= NG_POST < - 电子邮件联系表格

<an*********@yahoo.comwrote in message
news:11*********************@i3g2000cwc.googlegrou ps.com...
: I have a template array class on T with specialization for bloat when
T
: is void*. Thus when T is really an int*, my base ptr is physically a
: void** and needs to be converted to an int**.
:
: This doesn''t work:
:
: void** base = ...;
: int** ibase = static_cast<int**>(base); // illegal
:
: and this is bad practice:
:
: void** base = ...;
: int** ibase = reinterpret_cast<int**>(base);
:
: What am I supposed to do?

In theory, a void* may have a different in-memory representation
than int* -- even though conversions between the types are well
defined:
int i = 5;
void* p = &i;
*(int*)p = 6; // ok, i is now 6
**(int**)&p = 7; // undefined behavior !!

So if you want strictly portable code, you need to make sure
that the common implementation with void* arrays is always
addressed as a void* array.
: I''ve been warned away from all use of
: reinterpret_cast on the moderated group, and this seems to be a newbie
: question.

Specializing a template to use a common implementation is not
that much of a newbie question. It used to be more commonly
done a few years ago.
Note that some compilers/linkers today will automatically ''merge''
identical functions, and automatically take care of eliminating
the code bloat that you are trying to avoid.
hth -Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form




Victor Bazarov写道:

Victor Bazarov wrote:

发布更多代码。
Post more code.



模板< typename T>

class PrivateArray {

T * ar;


public:

T * Ptr(int i)const

{

返回ar + i;

}

};


模板< typename T>

class数组:public PrivateArray< T {

public:

T&am磷; operator [](int i)const

{

return * Ptr(i);

}

} ;


//专门化

模板< typename T>

class Array< T * {

PrivateArray< void * ar;


public:


T& operator [](int i)const

{

return * static_cast< T *>(ar.Ptr(i)); //非法???

}

};

template <typename T>
class PrivateArray {
T* ar;

public:
T* Ptr (int i) const
{
return ar+i;
}
};

template <typename T>
class Array : public PrivateArray<T{

public:
T& operator [] (int i) const
{
return *Ptr(i);
}
};

// specialize
template <typename T>
class Array<T*{
PrivateArray<void*ar;

public:

T& operator [] (int i) const
{
return *static_cast<T*>(ar.Ptr(i)); // illegal???
}
};


这篇关于void **到int **所需的强制转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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