是这类POD? [英] is this class POD ?

查看:46
本文介绍了是这类POD?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

模板< class T,无符号大小>

struct Array {

typedef unsigned SizeType;

typedef T ValueType;

typedef T * Ptr;

typedef T const * ConstPtr;


//默认复制ctor和赋值操作都没问题。


SizeType Size()const {返回大小; }


T& operator [](SizeType i){return m_array [i]; }

T const& operator [](SizeType i)const {return m_array [i]; }


Ptr Begin(){return& m_array [0]; }

ConstPtr Begin()const {return& m_array [0]; }


Ptr End(){return& m_array [0] + size; }

ConstPtr End()const {return& m_array [0] + size; }


私人:

ValueType m_array [size];

};


它既不定义副本也不定义析构函数。

私有部分是否存在为非POD?


我认为,如果上面的类是一个POD类型,在类型T和U之后的对象
最终将具有相同的内存布局:


typedef float TX [3] ;

typedef TX TY [4];


typedef数组< float,3> UX;

typedef数组< UX,4> UY;


typedef TY T;

typedef UY U;


谢谢。

-

:: bartekd [at] o2 [dot] pl

解决方案

bartek写道新闻:Xn ************************************** @ 153.19.2 51.200 in

comp.lang.c ++:


主题:这个类是POD吗?

模板<类T,无符号大小>
struct Array {


[snip POD-ish stuff]

private:
ValueType m_array [size];
};

它既不定义副本也不定义析构函数。
私有部分是否存在为非POD?


私有section不是问题,它是private * data *。

我假设,如果上面的类是POD类型,那么类型T和U的对象将会结束具有相同的内存布局:

typedef float TX [3];
typedef TX TY [4];

typedef A rray< float,3> UX;
typedef数组< UX,4> UY;

typedef TY T;
typedef UY U;




我不是很了解上述情况,但它无关紧要

作为数组<>不是POD。


不幸的是,POD只是作为一个概念存在,以提供与B(没有++)如何做事情的兼容性,有用的用途

超出此范围是罕见的。


Rob。

-
http://www.victim-prime.dsl.pipex.com/


Rob Williscroft< rt*@freenet.co.uk>写在

新闻:Xn ********************************** @ 130.133。 1.4:

bartek在新闻中写道:Xn ************************** ********@153.19.2 51.200 in
comp.lang.c ++:

主题:这是POD类吗?

template< class T,unsigned size>
struct Array {



[snip POD-ish stuff]


private:
ValueType m_array [size];
};

它既不定义copy-ctor也不定义析构函数。
是否存在私有部分呈现它作为非POD?



私有部分不是问题,它是私有*数据*。




这就是我的意思。

在这种情况下,公开数据会导致Array类

变得符合POD标准吗?
< blockquote class =post_quotes>

我假设,如果上面的类是POD类型,则跟随类型T和U的对象最终会得到相同的内存布局:

typedef float TX [3];
typedef TX TY [4];

typedef数组< float,3> UX;
typedef数组< UX,4> UY;

typedef TY T;
typedef UY U;



我真的不明白上面的内容,但它并不是物品
如数组<>不是POD。

不幸的是,POD只是作为一个概念存在,以提供与C(没有++)如何做事的兼容性,有用的用途
超出这个范围是罕见的。




很抱歉用一种有点人为的方式。

确实C兼容性就是我要去的。


说,我有一个C库,在其

接口中使用类似的typedef:


typedef float my_vector_t [3 ];

typedef float my_matrix_t [4] [4];


我需要使用C ++代码中的那些接口。

虽然,我想在我的界面中不使用C数组,所以将数组包装到类中的
似乎是一个合理的解决方案。而且,如果那些C ++包装的数组与C数组二进制兼容,那么我可以获得性能和统一的



如果我确定以下C ++对象和C数组具有相同的

二进制表示,我可以摆脱

接口边界上的显式转换,并获得使用严格的类型

我的程序。


float x [4] [4];

数组<数组<浮点数, 4>,4> y;


-

:: bartekd [at] o2 [dot] pl

bartek在新闻中写道:Xn ********************************** @ 153.19.2 51.200 < comp.lang.c ++中的

私有部分不是问题,私有*数据* 。
这就是我的意思。
在这种情况下,公开数据会导致Array类变得符合POD吗?




是的。




[snip]


不幸的是,POD只是作为一个概念存在,以提供与C(没有++)如何做事的兼容性,除此之外的有用用途很少见。



很抱歉用一种有点人为的方式。
确实C兼容性就是我要去的。

说,我有一个使用类似打字的C库在它的
界面中使用efs:

typedef float my_vector_t [3];
typedef float my_matrix_t [4] [4];

我需要使用来自C ++代码的那些接口。
虽然,我想在我的接口中不使用C数组,所以将数组包装到类中似乎是一个合理的解决方案。而且,如果那些C ++包装的数组与C数组二进制兼容,我可以同时获得性能和统一。

如果我确定以下C ++对象和C数组具有等效的二进制表示,我可以摆脱界面边界上的明确转换,并获得在我的程序中使用
严格类型的冒险。

浮动x [4] [4];
数组<数组< float,4>,4> y;




例如


struct xplusplus

{

浮动x [4] [4];

//无论如何......

};


IIU(标准版) C,x在xplusplus的偏移0处,但是你可以找到一个平台,其中sizeof(xplusplus)> sizeof(x)。


所以也许:


数组<数组<浮点数,4>,4> & y = *

reinterpret_cast< Array< Array< float,4>,4> *>(x)

;


工作正常,直到你尝试复制数组。


Don不做它的UB。如果你的代码正常工作,你的代码就会绑定到你当前的平台和你当前的编译器。


如果你展示一些C API的样本你的处理那么

我自己或其他人可以提供一些更好的建议

如何最好地处理它。


Rob。

-
http ://www.victim-prime.dsl.pipex.com/


template <class T, unsigned size>
struct Array {
typedef unsigned SizeType;
typedef T ValueType;
typedef T* Ptr;
typedef T const* ConstPtr;

// Default copy ctor and assignment op are fine.

SizeType Size() const { return size; }

T& operator[](SizeType i) { return m_array[i]; }
T const& operator[](SizeType i) const { return m_array[i]; }

Ptr Begin() { return &m_array[0]; }
ConstPtr Begin() const { return &m_array[0]; }

Ptr End() { return &m_array[0] + size; }
ConstPtr End() const { return &m_array[0] + size; }

private:
ValueType m_array[size];
};

It defines neither a copy-ctor nor a destructor.
Does the presence of private section render it as non-POD?

I assume that, if the above class was a POD type, objects of the
following types T and U would end up with equivalent memory layout:

typedef float TX[3];
typedef TX TY[4];

typedef Array<float, 3> UX;
typedef Array<UX, 4> UY;

typedef TY T;
typedef UY U;

Thanks.
--
:: bartekd [at] o2 [dot] pl

解决方案

bartek wrote in news:Xn**********************************@153.19.2 51.200 in
comp.lang.c++:

Subject: is this class POD ?

template <class T, unsigned size>
struct Array {
[snip POD-ish stuff]

private:
ValueType m_array[size];
};

It defines neither a copy-ctor nor a destructor.
Does the presence of private section render it as non-POD?

The private section isn''t the problem, its the private *data*.
I assume that, if the above class was a POD type, objects of the
following types T and U would end up with equivalent memory layout:

typedef float TX[3];
typedef TX TY[4];

typedef Array<float, 3> UX;
typedef Array<UX, 4> UY;

typedef TY T;
typedef UY U;



I don''t really understand the above, but it doesn''t matter
as Array<> isn''t a POD.

Unfortunatly POD only really exists as a concept to provide
compatibility with how C ( no ++ ) does things, useful uses
beyond that are rare.

Rob.
--
http://www.victim-prime.dsl.pipex.com/


Rob Williscroft <rt*@freenet.co.uk> wrote in
news:Xn**********************************@130.133. 1.4:

bartek wrote in
news:Xn**********************************@153.19.2 51.200 in
comp.lang.c++:

Subject: is this class POD ?

template <class T, unsigned size>
struct Array {



[snip POD-ish stuff]


private:
ValueType m_array[size];
};

It defines neither a copy-ctor nor a destructor.
Does the presence of private section render it as non-POD?



The private section isn''t the problem, its the private *data*.



That''s what I actually meant.
In that case, making the data public would result in the Array class
becoming POD-compliant?

I assume that, if the above class was a POD type, objects of the
following types T and U would end up with equivalent memory layout:

typedef float TX[3];
typedef TX TY[4];

typedef Array<float, 3> UX;
typedef Array<UX, 4> UY;

typedef TY T;
typedef UY U;



I don''t really understand the above, but it doesn''t matter
as Array<> isn''t a POD.

Unfortunatly POD only really exists as a concept to provide
compatibility with how C ( no ++ ) does things, useful uses
beyond that are rare.



Sorry for putting it in a somewhat contrived way.
Indeed C compatibility is what I''m going after.

Say, I have a C library which uses similar typedefs around in its
interfaces:

typedef float my_vector_t[3];
typedef float my_matrix_t[4][4];

I need to use those interfaces from C++ code.
Though, I''d like to keep out from using C arrays in my interfaces, so
wrapping arrays into a class seems a reasonable solution. Moreover, if
those C++ wrapped arrays were binary-compatible with C arrays, I could
gain both performance and unification.

If I was sure that the following C++ objects and C arrays had equivalent
binary representations, I could get rid of explicit conversions on
interface borders, and get the adventage of using strict types throughout
my program.

float x[4][4];
Array<Array<float, 4>, 4> y;

--
:: bartekd [at] o2 [dot] pl


bartek wrote in news:Xn**********************************@153.19.2 51.200
in comp.lang.c++:

The private section isn''t the problem, its the private *data*.
That''s what I actually meant.
In that case, making the data public would result in the Array class
becoming POD-compliant?



Yes.



[snip]


Unfortunatly POD only really exists as a concept to provide
compatibility with how C ( no ++ ) does things, useful uses
beyond that are rare.



Sorry for putting it in a somewhat contrived way.
Indeed C compatibility is what I''m going after.

Say, I have a C library which uses similar typedefs around in its
interfaces:

typedef float my_vector_t[3];
typedef float my_matrix_t[4][4];

I need to use those interfaces from C++ code.
Though, I''d like to keep out from using C arrays in my interfaces, so
wrapping arrays into a class seems a reasonable solution. Moreover, if
those C++ wrapped arrays were binary-compatible with C arrays, I could
gain both performance and unification.

If I was sure that the following C++ objects and C arrays had
equivalent binary representations, I could get rid of explicit
conversions on interface borders, and get the adventage of using
strict types throughout my program.

float x[4][4];
Array<Array<float, 4>, 4> y;



eg

struct xplusplus
{
float x[4][4];
// whatever ...
};

IIU( The Standard )C, x is at offset 0 of xplusplus, but you may
be able to find a platform where sizeof( xplusplus ) > sizeof( x ).

So maybe:

Array<Array<float, 4>, 4> &y = *
reinterpret_cast< Array<Array<float, 4>, 4> * >( x )
;

Will work Ok until you try copying your Array.

Don''t do it its UB. If you get your code working, your tying
your code to your current platform and your current compiler.

If you show some samples of the C API your dealing with then
myself or others may be able to give some better advice on
how best to deal with it.

Rob.
--
http://www.victim-prime.dsl.pipex.com/


这篇关于是这类POD?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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