i = i ++ for class types ...仍未定义? [英] i = i++ for class types...still undefined?

查看:58
本文介绍了i = i ++ for class types ...仍未定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近正在阅读一篇关于使用规范的序列点的文章


i = i ++;


作为修改变量的说明序列之间多次

积分。


奇怪的是文章没有说明我的类型。


是可以公平地说,如果我是一个类型的实例超载

operator ++(int)我们不再处理未定义的行为?


我的理由是,在那种情况下


i = i ++;


实际上是


i。 operator =(i.operator ++(0));


我并没有在序列点之间多次修改。


任何人都可以对此有所了解?


I was recently reading an article about sequence points that used the canonical

i = i++;

as an illustration of modifying a variable multiple times between sequence
points.

Curiously the article did not indicate the type of i.

Is it fair to say that if i was an instance of a class type that overloaded
operator++(int) that we are no longer dealing with undefined behavior?

My reasoning is that in that case

i = i++;

is actually

i.operator=(i.operator++(0));

and i is not really being modified multiple times between sequence points.

Can anyone shed some light on this?


推荐答案

" DaKoadMunky" <哒********* @ aol.com>在消息中写道

news:20 *************************** @ mb-m02.aol.com。 ..
"DaKoadMunky" <da*********@aol.com> wrote in message
news:20***************************@mb-m02.aol.com...
我最近正在阅读一篇关于使用
规范的序列点的文章
i = i ++;

作为修改变量的说明序列点之间多次。

奇怪的是文章没有说明我的类型。

如果我是一个实例
重载operator ++(int)的类类型我们不再处理未定义的行为?

我的理由是,在那种情况下

i = i ++;

实际上是i.operator =(i.operator ++(0));

我并没有在序列点之间多次修改。

任何人都可以对此有所了解吗?
I was recently reading an article about sequence points that used the canonical
i = i++;

as an illustration of modifying a variable multiple times between sequence
points.

Curiously the article did not indicate the type of i.

Is it fair to say that if i was an instance of a class type that overloaded operator++(int) that we are no longer dealing with undefined behavior?

My reasoning is that in that case

i = i++;

is actually

i.operator=(i.operator++(0));

and i is not really being modified multiple times between sequence points.

Can anyone shed some light on this?




听起来你不需要任何光照。确实,即使运算符重载使代码看起来类似,内部类型可能发生的某些未定义行为也会发生一些

类型类型。

在上面的代码中,函数调用创建序列点,如果我的类型为int,则不会出现
,因此没有未定义的行为。 />
假设我的operator ++和operator =的定义与int'相同,

首先增加值,然后将旧值传递给

operator =,保持不变。


-

David Hilsee



It sounds like you don''t need any light shed on this. It''s true that some
of the undefined behavior that can occur with built-in types does not occur
with class types even when operator overloading makes the code look similar.
In the above code, the function calls create sequence points that would not
be present if i had been of type int, so there is no undefined behavior.
Assuming that i''s operator++ and operator= are defined the same as int''s,
first the value is incremented and then the old value is passed to
operator=, leaving i unchanged.

--
David Hilsee


DaKoadMunky写道:
DaKoadMunky wrote:
我最近正在阅读一篇关于使用
规范的序列点的文章
i = i ++;

序列
点之间多次修改变量的说明。

奇怪的是文章没有说明我的类型。
重载了operator ++(int),我们不再处理未定义的行为?

我的理由是那个案子

i = i ++;

实际上是i.operator =(i.operator ++(0));

并且我并没有在序列点之间多次修改。


不错的问题。

有谁可以对此有所了解?
I was recently reading an article about sequence points that used the canonical
i = i++;

as an illustration of modifying a variable multiple times between sequence
points.

Curiously the article did not indicate the type of i.

Is it fair to say that if i was an instance of a class type that overloaded operator++(int) that we are no longer dealing with undefined behavior?

My reasoning is that in that case

i = i++;

is actually

i.operator=(i.operator++(0));

and i is not really being modified multiple times between sequence points.
Nice question.
Can anyone shed some light on this?




我''我不是语言律师,而且我也不会在teevee上玩一个......


....但是i.operator ++里面并没有强制序列点( )和

i.operator =()?


-

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


DaKoadMunky发布:
DaKoadMunky posted:
我的理由是在那种情况下

i = i ++;

实际上是i.operator =(i.operator ++(0));

我并没有在序列之间多次修改
点数。

任何人都可以对此有所了解吗?
My reasoning is that in that case

i = i++;

is actually

i.operator=(i.operator++(0));

and i is not really being modified multiple times between sequence
points.

Can anyone shed some light on this?




首先要做的事情:


签署主要()

{

int i = 5;


i = i ++;


std :: cout<< i;

}


我不会称之为上述未定义的行为,更像是实现 -

定义的行为。


签名main()

{

SomeClass obj;


obj = obj ++ ;

}

如果obj = obj ++变为:


obj.operator =(i.operator ++(0));


然后你说得对,

标准清楚地定义了什么,并且根本没有实现定义。

这就是我的样子。

-JKop



Well first thing:

signed main()
{
int i = 5;

i = i++;

std::cout << i;
}

I wouldn''t call the above undefined behaviour, more like implementation-
defined behaviour.

signed main()
{
SomeClass obj;

obj = obj++;
}
If obj = obj++ becomes:

obj.operator=( i.operator++(0) );

then you are right in saying that what happens is clearly defined by the
Standard and is not implementation-defined at all.

That''s what it looks like to me.
-JKop


这篇关于i = i ++ for class types ...仍未定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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