- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我想弄清楚 C++11 规范中是否有关于 re.以下代码 ( GitHub link here ) 的预期行为:
struct Scalar {
int data;
Scalar(int x) : data(x) {}
int get() {
return data;
}
Scalar &square() {
scale(data);
return *this;
}
void scale(int rhs) {
data *= rhs;
}
};
int main() {
Scalar v(3);
v.square().scale(v.get());
return v.data;
}
这主要是因为发现这在 g++
和 clang++
之间做了不同的事情:
$ g++ --version
g++ (GCC) 6.2.1 20160830
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ clang++ --version
clang version 3.9.0 (tags/RELEASE_390/final)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
$ g++ -std=c++11 order_of_ops.cpp -o a.out && ./a.out; echo $?
27
$ clang++ -std=c++11 order_of_ops.cpp -o a.out && ./a.out; echo $?
81
答案似乎应该在n3242 的§ 5.2.2 和§ 5.2.5 中。但我无法找到具体的东西。
最佳答案
如果我没看错,你的代码的行为是未指定的。 C++11 引号的 N3337:
§ 1.9 [intro.execution] / 15
Except where noted, evaluations of operands of individual operators and of subexpressions of individual expressions are unsequenced. [...] If a side effect on a scalar object is unsequenced relative to either another side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined.
但紧随其后的是
Every evaluation in the calling function (including other function calls) that is not otherwise specifically sequenced before or after the execution of the body of the called function is indeterminately sequenced with respect to the execution of the called function.9
9) In other words, function executions do not interleave with each other.
和
§ 5.2.2 [expr.call] / 8
[ Note: The evaluations of the postfix expression and of the argument expressions are all unsequenced relative to one another. All side effects of argument expression evaluations are sequenced before the function is entered (see 1.9). —end note ]
因此,您对 Scalar::data
的修改和不相关的读取是不确定的顺序。
话虽这么说,它很可能会改变并在 C++1z 中得到明确定义:
N4606 § 5.2.2 [expr.call] / 5
The postfix-expression is sequenced before each expression in the expression-list and any default argument. The initialization of a parameter, including every associated value computation and side effect, is indeterminately sequenced with respect to that of any other parameter.
因此,在 C++1z 中,您的 v.data
应该等于 81(如果我没看错的话)
关于C++ 求值顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40827303/
for (i = 0; i <= 1000; i++) { if ( i % 3 === 0){ console.log(i); } if ( i % 5 ==
对于一项作业,我需要解决一个数学问题。我将其缩小为以下内容: 令 A[1, ... ,n] 为 n 整数数组。 令y 为整数常量。 现在,我必须编写一个算法,在 O(n) 时间内找到 M(y) 的最小
我可以使用 iOS MediaPlayer 并通过这种方式播放电影。但我需要,寻找一秒钟的电影。我该怎么做,我像这样通过 MediaPlayer 播放电影: NSURL *videoURL =
我听说过 eCos看起来作为一个爱好项目来玩会很有趣。 任何人都可以推荐一个价格合理的开发板。如果它不会增加太多成本,我想要几个按钮来按下(并以编程方式检测按下)和一些调试输出的 LCD。以太网会很好
给定 a 到 b 的范围和数字 k ,找到 a 到 b [包括两者]之间的所有 k-素数。 k-素数的定义:如果一个数恰好有 k 个不同的素数因子,则该数是 k-素数。 即 a=4 , b=10 k=
这是对 my previous question 的重新措辞尝试作为它收到的反馈的结果。 我想要一个简单的网络通信,我可以将其用作底层框架,而无需再次查看。我只想将一个字符串从一台 PC 推送到另一台
我有许多节点通过其他类型的中间节点连接。如图所示,中间节点可以有多个。我需要找到给定数量的节点的所有中间节点,并按初始节点之间的链接数量对其进行排序。在我的示例中,给定 A、B、C、D,它应该返回节点
我的代码遇到问题。我试图找到这个 5x5 数组的总和,但它总是给我总计 0。当我使用 2x2 数组时,它可以工作,但对于 5x5 数组则不起作用。有人可以帮忙吗? import java.util.*
我们有一个给定的数组,我们想要打印 BST 中每个节点的级别。 例如,如果给定数组为:{15, 6, 2, 10, 9, 7, 13} 那么答案是: 1 2 3 3 4 5 4 (表示存储15的节点级
我对 R 和编程非常陌生,所以请留在我身边:) 我正在尝试使用迭代来查找无限迭代到小数点后第四位的值。 IE。其中小数点后第四位不变。所以 1.4223,其中 3 不再改变,所以小数点后 3 位的结果
我的问题与 Fastest way of computing the power that a "power of 2" number used? 非常相似: 将 x=2^y 作为输入,我想输出 y。
如何找到三个非零数字中最小的一个。 我尝试引入一个非常小的数字eps = 1e-6(我的数字为零或明显大于eps)并在min(x,eps)、min(y,eps)之间进行测试)等我什么也没得到。有没有办
我有一个类(class),他们计算矩阵中最大的“1”岛,但他的岛概念是“如果两个单元在水平、垂直或对角线上彼此相邻,则称它们是相连的。 “ 我需要帮助来删除对角台阶。 class GFG {
我开始使用 IDE Jupyter && Python 3.6 并出现了一个问题。我必须通过IDE绘制Petersen子图中的哈密顿路径,但我不知道该怎么做。 我显示有关该图的信息: Petersen
public static void main(String[] args) { int sum = 2; int isPrime; for(int x = 3; x Mat
这个问题已经有答案了: 已关闭10 年前。 Possible Duplicate: How much time should it take to find the sum of all prime
我想找到给定节点到链表二叉搜索树中根的距离。我有下面的代码来计算树的高度(root.getHeightN()),从根到叶子,但我现在需要的是从叶子到根。 public int getHeightN()
是否有一种优雅的方法使用预先计算的 KDTree 来查找连接组件的数量?现在使用呼吸优先搜索算法以及 k 最近邻的 KDTree 给出的邻接矩阵来查找连接的组件,但是是否有更好的可能性? import
我有一个要求,我需要找到具有相同名称的不同对象中 amt 值的总和。下面是代码片段 traveler = [ { description: 'Senior', Amount: 50}, {
我正在尝试使用 pandas 对某些列进行求和,同时保留其他列。例如: member_no, data_1, data_2, data_3, dat_1, dat_2, other_1, other_
我是一名优秀的程序员,十分优秀!