这是便携式的 [英] is this portable

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

问题描述

你好。此代码是否可在平台之间移植?它是否也是100%标准

兼容?

#include< iostream>

使用命名空间std;


class Point {

public:

enum COORDS {X = 0,Y,Z};

Point (int x,int y,int z):_ x(x),_ y(y),_ z(z){}

const int& operator [](COORDS c)const

{return(& _x)[c]; }


私人:

int _x,_ y,_z;

};


int main(){

点p(1,2,3);


cout<< p [Point :: X]<< endl;

cout<< p [Point :: Y]<< endl;

cout<< p [Point :: Z]<< endl;

}


是否保存以获取第一个成员的地址,并对其执行指针算术

得到所有3个元素?


Thanx

Martin


[见 http://www.gotw.ca/resources/clcm.htm 有关的信息]

[comp.lang.c ++。moderated。第一次海报:做到这一点! ]

解决方案



" Martin Vorbrodt" < MV ******* @ poczta.onet.pl>在消息中写道

新闻:ck ********** @ news.onet.pl ...

你好。此代码是否可在平台之间移植?它是否100%
标准




不,不。


但是你为什么要写这样的代码?


john


John Harrison写道:

Martin Vorbrodt写道:

你好。此代码是否可在平台之间移植?是否100%
标准符合要求?



不,不。




数据成员在一个private:标签内,所以他们的订单是定义良好的,并且他们的填充是实现定义的。


第二条规则使它们成为非便携式。


所以,约翰,为什么他们不符合100%标准?

但你为什么要写这样的代码?




因为OpenGL奖励你。它允许其方法的许多变体来使用一个索引数组作为基本的点对象。


建议OP断言() Point的大小等于

三个整数的大小,并继续前进。除非约翰可以把他说出来。


-

Phlip
http://industrialxp.org/community/bi...UserInterfaces


* Phlip:

John Harrison写道:

Martin Vorbrodt写道:


你好。此代码是否可在平台之间移植?是否100%
标准符合要求?



不,不。



所以,John,为什么它们不是100%标准兼容吗?




由于无效的指针算法,程序具有未定义的效果

(通过索引)。我的经验是,你可能会通过要求进一步的理由来回应

。并且要提前响应

,请查看有效指针值的规则。

但是你为什么要编写这样的代码?



因为OpenGL会奖励你。它允许其方法的许多变体将索引数组作为基本点对象。

建议OP断言()Point的大小等于大小
三个整数,继续前进。除非约翰可以告诉他。




这是非常糟糕的建议,因为不需要未定义的效果。


-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet和电子邮件中最烦人的事情是什么?


Hi there. Is this code portable between platforms? Is it also 100% standard
compliant?

#include <iostream>
using namespace std;

class Point {
public:
enum COORDS { X = 0, Y, Z };
Point(int x, int y, int z) : _x(x), _y(y), _z(z) {}

const int& operator[](COORDS c) const
{ return (&_x)[c]; }

private:
int _x, _y, _z;
};

int main() {
Point p(1, 2, 3);

cout << p[Point::X] << endl;
cout << p[Point::Y] << endl;
cout << p[Point::Z] << endl;
}

Is it save to get the address of the first member, and do pointer arithmetic
on it to get to all 3 elements?

Thanx
Martin

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

解决方案


"Martin Vorbrodt" <mv*******@poczta.onet.pl> wrote in message
news:ck**********@news.onet.pl...

Hi there. Is this code portable between platforms? Is it also 100%
standard
compliant?



No, and no.

But why would you want to write code like this?

john


John Harrison wrote:

Martin Vorbrodt wrote:

Hi there. Is this code portable between platforms? Is it also 100%
standard
compliant?



No, and no.



The data members were within one private: tag, so their order is
well-defined, and their paddings are implementation-defined.

The second rule makes them non-portable.

So, John, why are they not 100% standard compliant?
But why would you want to write code like this?



Because OpenGL rewards you to. It permits many variations of its methods to
take an array of indices as a primitive "point object".

The OP is advised to assert() that the size of Point equals the size of
three ints, and keep going. Unless if John can talk him out of it.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces


* Phlip:

John Harrison wrote:

Martin Vorbrodt wrote:


Hi there. Is this code portable between platforms? Is it also 100%
standard
compliant?



No, and no.



So, John, why are they not 100% standard compliant?



The program has undefined effect due to invalid pointer arithmetic
(via indexing). My experience is that you''ll probably respond to
that by demanding some further justification. And to respond to
that response in advance, look up the rules for valid pointer values.

But why would you want to write code like this?



Because OpenGL rewards you to. It permits many variations of its methods to
take an array of indices as a primitive "point object".

The OP is advised to assert() that the size of Point equals the size of
three ints, and keep going. Unless if John can talk him out of it.



That''s very bad advice because undefined effect is unnecessary.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


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

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