模棱两可的表达 [英] Ambiguous Expression

查看:95
本文介绍了模棱两可的表达的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

同事,


假设我(当然是简化形式)


struct Vect2D {

double x,y;

Vect2D(double x_,double y_):x(x_),y(y_){}

};


我必须用随机数构建:


Vect2D v(rand(),rand());


但这是模糊表达。 - 即......语言没有

保证函数调用的参数的评估顺序为




我确实被这种类型的代码打败了 - 释放和调试构建

表现不同:(


以上代码是如此典型,我有吗强制论证的明确顺序

评价?它不会那么紧凑和美观。

-

弗拉基米尔

解决方案

弗拉基米尔写道:

同事,

假设我有(简化形式,当然)

struct Vect2D {
double x,y;
Vect2D(double x_,double y_):x(x_),y(y_){}
} ;

我必须用随机数构建:

Vect2D v(rand(),rand());

但这是 ;模糊表达 - 即......语言不保证函数调用的参数评估的顺序
评估d。

我确实被这种类型的代码打败了 - 释放和调试构建
表现不同:(

以上代码是如此典型,我是必须强制明确的论证评估顺序?它不会那么紧凑和美观。




你当然可以。你总是要考虑功能的副作用

来电。但是,请注意,当_random_值关注时,人们不应该关心

顺序,如果有的话?


V


Victor Bazarov写道:

但是,请注意,当_random_值涉及时,不应该关心
顺序,应该吗?


我也这么认为:)

但实际问题不是订单本身。具有

全局优化和内联的现代编译器将达到类似


调用的情况(++ i,++ i);

这可能导致两个参数都相同!

这对我来说并不是随机的:)

Vect2D v(rand(),rand());



当然可以。你总是要考虑



函数调用的副作用。




但是C ++在允许使用东西方面非常强大在飞行中,

真的有助于大型程序。什么可以是最紧凑的,最好是这个问题的单线解决方案呢?

-

弗拉基米尔


"弗拉基米尔" < VL ********* @ yahoo.com>写道:

Victor Bazarov写道:

但是,请注意,当_random_值涉及时,不应该关心
顺序,应该吗?



我也这么认为:)
但实际问题不是订单本身。具有全局优化和内联的现代编译器将达到类似

调用(++ i,++ i);

这样的情况,这可能导致两个参数都存在同样的!
这对我来说并不是随机的:)




上面的表达式有未定义的行为,因为对象我有

它的_stored_value_在序列点之间改变了不止一次。

> Vect2D v(rand(),rand());



当然可以。你总是要考虑


函数

调用的副作用。



但是C ++在允许使用东西方面非常强大在飞行中,它确实有助于大型项目。什么可以是最紧凑的,最好是这个问题的单线解决方案呢?




没有。使用单独声明/定义/初始化的对象:


int r1 = rand(),r2 = rand();

Vect2D v(r1,r2);


Victor


Colleagues,

Suppose I have (in simplified form, of course)

struct Vect2D {
double x, y;
Vect2D(double x_, double y_) : x(x_), y(y_) {}
};

which I have to construct with random numbers:

Vect2D v(rand(), rand());

But this is "Ambiguous Expression" - i.e. "...language does not
guarantee the order in which arguments to a function call are
evaluated."

I was beaten by such type of code indeed - release and debug builds
behave differently :(

Above code is so typical, do I have to force explicit order of argument
evaluation? It would not be so compact and nice.
--
Vladimir

解决方案

Vladimir wrote:

Colleagues,

Suppose I have (in simplified form, of course)

struct Vect2D {
double x, y;
Vect2D(double x_, double y_) : x(x_), y(y_) {}
};

which I have to construct with random numbers:

Vect2D v(rand(), rand());

But this is "Ambiguous Expression" - i.e. "...language does not
guarantee the order in which arguments to a function call are
evaluated."

I was beaten by such type of code indeed - release and debug builds
behave differently :(

Above code is so typical, do I have to force explicit order of argument
evaluation? It would not be so compact and nice.



Of course you do. You always have to account for side effects of function
calls. However, let me note here that one shouldn''t really care about the
order when _random_ values are concerned, should one?

V


Victor Bazarov wrote:

However, let me note here that one shouldn''t really care about the
order when _random_ values are concerned, should one?
I thought so too :)
But the actual problem isn''t the order itself. Modern compilers with
global opimizations and inlining will reach a situation like

call(++i, ++i);

which can result in both arguments being the same!
That''s not really random to me :)

Vect2D v(rand(), rand());



Of course you do. You always have to account for side effects of


function calls.



But C++ is quite powerful in allowing to use stuff on the fly which
really helps in large programs. What can be most compact, preferably
one-line solution to this problem then?
--
Vladimir


"Vladimir" <vl*********@yahoo.com> wrote...

Victor Bazarov wrote:

However, let me note here that one shouldn''t really care about the
order when _random_ values are concerned, should one?



I thought so too :)
But the actual problem isn''t the order itself. Modern compilers with
global opimizations and inlining will reach a situation like

call(++i, ++i);

which can result in both arguments being the same!
That''s not really random to me :)



The expression above has undefined behaviour because the object ''i'' has
its _stored_value_ changed more than once between sequence points.

> Vect2D v(rand(), rand());



Of course you do. You always have to account for side effects of


function

calls.



But C++ is quite powerful in allowing to use stuff on the fly which
really helps in large programs. What can be most compact, preferably
one-line solution to this problem then?



There isn''t any. Use separately declared/defined/initialised objects:

int r1 = rand(), r2 = rand();
Vect2D v(r1, r2);

Victor


这篇关于模棱两可的表达的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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