C ++移位运算符优先级怪异 [英] C++ shift operator precedence weirdness

查看:85
本文介绍了C ++移位运算符优先级怪异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下代码:

typedef vector<int> intVec;

intVec& operator<<(intVec& dst, const int i) {
    dst.push_back(i);
    return dst;
}
int intResult0() {
    return 23;
}
int intResult1() {
    return 42;
}

// main
intVec v;
v << intResult0() << intResult1();

奇怪的是,编译器生成了代码,该代码会评估 intResult1 之前 intResult0 (已通过最新的VC和gcc测试).为什么编译器会这样做?通过这样做,(不必要地)增加(不必要地)评估和使用各个值之间的时间(Δ),即,首先获取42,但是最后将其推到向量.C ++标准规定了这一点吗?

The weird thing is, that the compiler generates code, which evaluates intResult1 BEFORE intResult0 (tested with newest VC und gcc). Why would the compiler do this? By doing so, the time between evaluation and usage of the respective values is (unnecessarily) increased(?), i.e. 42 is fetched first, but pushed last to the vector. Does the C++ standard dictate this?

推荐答案

根据Stroustrup第6.2.2节:

According to Stroustrup section 6.2.2:

的评估顺序表达式中的子表达式是未定义.

The order of evaluation of subexpressions within an expression is undefined.

这篇关于C ++移位运算符优先级怪异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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