Pitfalls: C++ 中的index运算符
名可名, 非常名
在调一个题目的过程中发现了一个之前从未意识到的问题.
考虑如下代码
vector<int> a;
int f(int x){
a.push_back(x);
return *a.rbegin();
}
a.push_back(0);
a[0]=f(1);
试问最后a[0]的值.
Unique Variable
a variable provides us with named storage that our programs can manipulate.
这里需要指出的是, a[0]是一个返回lvalue的expression, 它是一个variable, 但并不是一个unique variable.
(unique variable这个词是我造的, 下面将会解释其含义)
如果一个变量在它的lifetime中都指向 (refer to) 同一storage, 则称其为 unique variable.
Remember That Operands Can Be Evaluated in Any Order
Precedence specifies how the operands are grouped. It says nothing about the order in which the operands are evaluated. In most cases, the order is largely unspecified. In the following expression
int i = f1() * f2();
We know that f1 and f2 must be called before the multiplication can be done. After all, it is their results that are multiplied. However, we have no way of knowing whether f1 will be called before f2 or vice versa.
For operators that do not specify evaluation order, it is an error for an expression to refer to and change the same object. Expressions that do so have undefined behavior.
There are four operators that do guarantee the order in which operands are evaluated: the logical AND (&&) operator, the logical OR (||) operator, the conditional (?
Pitfalls: C++ 中的index运算符的更多相关文章
- SQL Server 2000中的并行处理和执行计划中的位图运算符
SQL Server 2000中的并行处理和执行计划中的位图运算符 摘抄自:SQLServer 2000并行处理和位图简介 刘志斌 并行查询介绍Degree of Parallelism(并行度) 一 ...
- java中的移位运算符:<<,>>,>>>总结
java中有三种移位运算符 << : 左移运算符,num << 1,相当于num乘以2 >> : 右移运算符,num >& ...
- ThinkPHP 隐藏URL中的 index.php
去掉 URL 中的 index.php 通常的URL里面含有index.php,为了达到更好的SEO效果可能需要去掉URL里面的index.php ,通过URL重写的方式可以达到这种效果,通常需要服务 ...
- js中的等值运算符(抽象相等==与严格相等===的区别)
js中的等值运算符 js中的相等分为抽象相等和严格相等,他们有什么区别呢. 在说具体算法前,先提下JS数据类型,JS数据类型分为6类:Undefined Null String Number Bool ...
- [.net 面向对象编程基础] (6) 基础中的基础——运算符和表达式
[.net 面向对象编程基础] (6) 基础中的基础——运算符和表达式 说起C#运算符和表达式,小伙伴们肯定以为很简单,其实要用好表达式,不是一件容易的事.一个好的表达式可以让你做事半功倍的效果,比如 ...
- Handlebars.js循环中索引(@index)使用技巧(访问父级索引)
使用Handlebars.js过程中,难免会使用循环,比如构造数据表格.而使用循环,又经常会用到索引,也就是获取当前循环到第几次了,一般会以这个为序号显示在页面上. Handlebars.js中获取循 ...
- Elasticsearch——禁止Body中的index覆盖Url中的index参数
本篇继续一下Elasticsearch日常使用的技巧翻译. 在Elasticsearch有很多的api支持在body中指定_index等信息,比如mget或者msearch以及bulk. 默认的情况下 ...
- Java中的移位运算符
java中有三种移位运算符 << : 左移运算符,num << 1,相当于num乘以2 >> : 右移运算符,num >& ...
- VB.NET中的除法运算符 与 C#中的除法运算符
VB.NET中的除法运算符有两个:/(浮点除法).\(整数除法) C#中的除法运算符只有一个:/(除法) VB.NET中的除法运算符与C#中的除法运算符存在很大的差异,使用时注意区分. 关于VB.NE ...
随机推荐
- IOS 网络-深入浅出(一 )-> 三方SDWebImage
首要我们以最为常用的UIImageView为例介绍实现原理: 1)UIImageView+WebCache: setImageWithURL:placeholderImage:options: 先显 ...
- 学习Maven之Maven Clean Plugin
1.maven-clean-plugin是个什么鬼? maven-clean-plugin这个插件用maven的人都不陌生.我们在执行命令mvn clean时调用的就是这个插件. 这个插件的主要作用就 ...
- css 垂直居中
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- nginx 安全优化
http://nginx.org/en/docs/http/ngx_http_access_module.html 官网 1.允许特定的ip访问,拒绝特定ip server { listen 80; ...
- ajax 允许跨域html头设置
"Access-Control-Allow-Origin"= "*" "Access-Control-Allow-Headers"= &qu ...
- 《一个操作系统的实现》 ubuntu系统环境配置
<一个操作系统的实现> ubuntu系统环境配置 电脑之前已经安装了gcc. 一.nasm安装:sudo apt-get install nasm或官网下载http://sourcefor ...
- Servlet编程
Servlet编程 1. servlet概念及相关接口简介 java Servlet是运行在web服务器或应用服务器上的程序,他是作为来自web浏览器或其他HTTP客户端的请求和HTTP服务器山的数据 ...
- java多线程系类:JUC线程池:06之Callable和Future(转)
概要 本章介绍线程池中的Callable和Future.Callable 和 Future 简介示例和源码分析(基于JDK1.7.0_40) 转载请注明出处:http://www.cnblogs.co ...
- [LeetCode] Count The Repetitions 计数重复个数
Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc&qu ...
- c#中多线程同步Lock(锁)的研究以及跨线程UI的操作
本文只针对C#中,多线程同步所用到的锁(lock)作为研究对象.由于想更直观的显示结果,所以,在做demo的时候,就把多线程通过事件操作UI的代码也写了出来,留作备忘和分享吧. 其实多线程的同步,使用 ...