- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
回答后this question , 关于所讨论的代码是否为未定义行为的问题进行了长时间的讨论。这是代码:
std::map<string, size_t> word_count;
word_count["a"] = word_count.count("a") == 0 ? 1 : 2;
首先,众所周知,这至少是未指定的。结果因首先评估作业的哪一侧而异。在我的回答中,我遵循了四个结果案例中的每一个,首先评估哪一方的因素以及该元素是否在此之前存在。
还有一个简短的表格出现了:
(x = 0) = (x == 0) ? 1 : 2; //started as
(x = 0) = (y == "a") ? 1 : 2; //changed to
我声称它更像这样:
(x = 0, x) = (x == 0) ? 1 : 2; //comma sequences x, like [] should
最终,我找到了一个似乎对我有用的例子:
i = (++i,i++,i); //well-defined per SO:Undefined Behaviour and Sequence Points
回到最初,我将其分解为相关的函数调用,以便于理解:
operator=(word_count.operator[]("a"), word_count.count("a") == 0 ? 1 : 2);
^ inserts element^ ^reads same element
|
assigns to element
如果 word_count["a"]
不存在,有人认为它会被分配给 twice 而中间没有排序。如果我认为是真实的两件事实际上是:
When a side is picked to be evaluated, the whole side has to be evaluated before the other side can start.
诸如 word_count["a"] = 1 的构造表现出明确定义的行为,即使在插入元素然后分配给的情况下也是如此。
这两个说法是真的吗?最终,这实际上是未定义的行为吗?如果是,为什么第二个语句有效(假设有效)?如果第二个为假,我相信世界上所有的 myMap[i]++;
都是病式的。
最佳答案
行为未指定,但并非未定义。
注意,在表达式中:
word_count["a"] = word_count.count("a") == 0 ? 1 : 2;
// ^
用^
标记的赋值运算符是内置赋值运算符,因为std::map
的运算符[ ]
返回一个 size_t&
。
根据 C++11 标准关于内置赋值运算符的第 5.17/1 段:
The assignment operator (=) and the compound assignment operators all group right-to-left. [..] In all cases, the assignment is sequenced after the value computation of the right and left operands, and before the value computation of the assignment expression. With respect to an indeterminately-sequenced function call, the operation of a compound assignment is a single evaluation.
这意味着在内置赋值中,例如:
a = b
首先对操作数进行求值(顺序不定),然后进行赋值,最后对整个赋值表达式进行值计算。
考虑原始表达式:
word_count["a"] = word_count.count("a") == 0 ? 1 : 2;
// ^
由于上面引用的段落,在任何情况下都不会对同一对象有两个未排序的赋值:标记为 ^
的赋值将始终在之后排序operator []
执行的赋值(作为左侧表达式求值的一部分),以防键 "a"
不存在于 map 。
但是,根据首先评估赋值的哪一侧,表达式将有不同的结果。因此,行为是未指定的,但不是未定义的。
关于c++ - 是在索引一个新的 map 元素并将读取它的东西分配给它未定义的行为,还是只是未指定的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15865627/
这个fn是吗: function isplainobj ( obj ) { return Object.prototype.toString.call( obj ) === "[object
我尝试创建一个我没有经验的小 bash 脚本。我尝试做类似的事情: #!/bin/bash statut="na" if [ $proc = 0 ]; then statut = "close
我想重写 HighLine 的几个方法来自定义我的控制台,目前我的代码如下所示: cmd = ask("#{@prompt_label} #{@prompt_separator} ",
鉴于下面的 HTML,我尝试使用 jQuery 来匹配所有具有类“foo”的跨度的列表项,并且该跨度应包含文本“relevant”。 Some text relevant Some more
我拥有一个 5 美元 20GB SSD Digital Ocean Droplet,它几乎用完了 Docker 镜像和容器的空间。 有没有办法购买一个卷,将其连接到服务器并安全地将所有 Docker
我有这样的表: id name number result stuff stuff stuff stuff 我只想将 class = "red" 添加到
我需要计算两点之间的距离,但不是以常规方式。我需要知道“东西距离”+“南北距离”。我想这比常规的“乌鸦飞翔”计算更简单,但我仍然不知道如何做到这一点。 我想使用 MySQL 查询来执行此操作,并且最好
#include #include #include typedef struct dict_pair { void *key; void *value; struct dict_p
为什么当我尝试通过 将 char[] word 写入控制台时会发生这种奇怪的事情 Console.WriteLine(word); 我得到了一个正确的结果,但是当我写的时候 Console.Write
一个简单的例子: class C{} class B{ @Inject C c; void doSomething(){ System.out.println(c);
我想做某事,但不确定如何描述它。我有这门课 public class Company { private List _persons; private Person GetPersonByNa
我正在尝试实现我自己的 qDebug()样式调试输出流,这基本上是我目前所拥有的: struct debug { #if defined(DEBUG) template std::os
所以我正在为我的应用程序编写一个搜索功能,代码如下: - (void) selectObject: (NSString *)notes{ [axKnotes removeAllObjects]
我想在 Rust 中匹配一种复杂的继承式东西: struct Entity { pub kind: EntityKind, } pub enum EntityKind { Player
我是 SQL 新手。这没有返回任何结果...... SELECT media.id as mediaid, media.title as mediatitle, media.description
在微型 SDCard 上写入 Android things 图像并将该卡插入 Raspberry Pi 3 Model B 并打开电源,启动时显示“Auto config Please wait”然后
这是一个常见的但是文本出现在框的右侧,不是极右但几乎是这样 h3: ................................................ .................
#include #include #include #include #include int main(int argc, string argv[]) { if(argc >
所以我试图让一些东西相互堆叠。首先,查看工作链接会有所帮助,您会看到问题所在: http://brownbox.net.au/clients/matchlessphotography/ 现在我需要使用
我想在禁用 javascript 时在我的网站顶部显示一条消息(就像在 SO 上一样),但在谷歌浏览器上不起作用 最佳答案 看起来是这样。 您可以使用 javascript 隐藏“noscript”消
我是一名优秀的程序员,十分优秀!