有效的C ++? [英] Valid C++?

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

问题描述



此代码是否在C ++下有效?我想使用这个结构,但我不是

确定它是否便携。


struct foo

{

char x [128];

};

struct bar

{

char sameSizeAsFooX [sizeof((foo *)0) - > x];

};


谢谢,

Andrew

解决方案



andrew queisser写道:

这是代码低于有效的C ++?我想使用这个结构,但我不确定它是否可移动。

struct foo
{
char x [ 128];
};
结构栏
{char相同尺寸AsFooX [sizeof((foo *)0) - > x];
};



是的,它是有效的。但是,我相信它也会导致未定义的

行为。


另一方面,它是一种常用的技术,尤其适用于C,用于

实例:


diff =(&((foo *)0) - > x - ((foo *)0))


或类似的东西。


大多数编译器都会创建符合您期望的代码。由于你没有从x读取或写入它只是在

指针变量上进行算术运算。


andrew queisser写道:

这段代码下面是否有效C ++?我想使用这个结构,但我不确定它是否可移动。

struct foo
{
char x [ 128];
};
结构栏
{char相同尺寸AsFooX [sizeof((foo *)0) - > x];
};



虽然这似乎有用,但为了清楚起见,我会以不同的方式来做这件事。最糟糕的是,这样的事情:


char sameSizeAsFooX [sizeof foo()。x];


更好,恕我直言,将是:


struct foo

{

enum {SIZE = 128};

char x [SIZE] ;

};


结构栏

{

char sameSizeAsFooX [foo :: SIZE] ;

};


干杯! --M


andrew queisser写道:

这段代码下面是否有效C ++?我想使用这个结构,但我不确定它是否可移动。

struct foo
{
char x [ 128];
};
结构栏
{char相同尺寸AsFooX [sizeof((foo *)0) - > x];


我猜,没关系。看起来相当危险,比如解除引用

一个空指针。也许它不那么可怕了


char sameSizeAsFoox [sizeof foo()。x];


(尽管确实需要' 'foo''是默认构造的,而

你的解决方案没有。)

};




V

-

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

我不回复热门帖子回复,请不要问



Is this code below valid C++? I''d like to use this construct but I''m not
sure if it''ll be portable.

struct foo
{
char x[128];
};
struct bar
{
char sameSizeAsFooX[ sizeof ((foo *)0)->x ];
};

Thanks,
Andrew

解决方案


andrew queisser wrote:

Is this code below valid C++? I''d like to use this construct but I''m not
sure if it''ll be portable.

struct foo
{
char x[128];
};
struct bar
{
char sameSizeAsFooX[ sizeof ((foo *)0)->x ];
};



Yes it is valid. However, I believe it also results in undefined
behavior.

On the other hand, it is a common technique to use especially in C, for
instance:

diff = (&((foo*)0)->x - ((foo*)0))

or something similar.

Most compilers create code that does what you expect here. Since you
are not reading from x or writing to it its just arith operations on a
pointer variable.


andrew queisser wrote:

Is this code below valid C++? I''d like to use this construct but I''m not
sure if it''ll be portable.

struct foo
{
char x[128];
};
struct bar
{
char sameSizeAsFooX[ sizeof ((foo *)0)->x ];
};



Though that seems to work, for the sake of clarity, I would do it
differently. At worst, something like this:

char sameSizeAsFooX[ sizeof foo().x ];

Better, IMHO, would be:

struct foo
{
enum { SIZE = 128 };
char x[SIZE];
};

struct bar
{
char sameSizeAsFooX[ foo::SIZE ];
};

Cheers! --M


andrew queisser wrote:

Is this code below valid C++? I''d like to use this construct but I''m
not sure if it''ll be portable.

struct foo
{
char x[128];
};
struct bar
{
char sameSizeAsFooX[ sizeof ((foo *)0)->x ];
It is OK, I guess. Seems rather dangerous though, like dereferencing
a null pointer. Perhaps it would be less scary to do

char sameSizeAsFoox[ sizeof foo().x ];

(although it does require for ''foo'' to be default-constructible while
your solution does not).
};



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


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

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