为了在自定义类里重载一个异常安全(exception safe)的赋值操作符,创造了一个这样的习惯用语。也叫:Create-Temporary-and-Swap。
要想写健壮的C++代码,异常安全非常重要。
可以给异常安全分三级:
1> 基本安全:实现简单、代价小。应该作为常规手段。
2> 很安全:在任何场合都实现这一步不太可能。本例的赋值操作符重载算是一个。
3> 不抛出异常:可以看non-throwing swap。
所谓“Create-Temporary-and-Swap”,就是先申请新资源,用后再释放。
1> 在申请新资源的时候,要使用RAII原则。
2> 申请成功之后,使用non-throwing swap原则交换资源。
有时会加入对参数的判断:
在函数内部的临时变量,其实可以省略,只要用传值的方式:
这种作法在某些情况下可以得到优化。
如果s绑定到lvalue,无优化,栈内会有临时对象被创建;如果绑定到rvalue,一般就会省略掉copy ctor的调用。
在C++0x标准中,这样的赋值操作符叫做“统一赋值操作符”,因为它合“拷贝赋值”、“移动赋值”为一体了。C++0x编译器一旦发现某个类有move ctor存在,总会对临时的rvalue进行优化的。
老编译器当然没有move ctor,但是能对赋值操作进行rvalue的优化,也算是有的一比。
分享到:
相关推荐
Booting and exploration: logins, consoles, swap space, partitioned disks, and more Copy-On-Write (COW): UML's efficient approach to storing filesystem changes In-depth discussion of User ...
Computer brand, model, and the brands and model numbers of any additional hardware. d. Operating system and version number. (The version number can be determined by typing VER at the MSDOS prompt...
I'll keep updating for full summary and better solutions. Stay tuned for updates. (Notes: "馃摉" means you need to subscribe to [LeetCode premium membership](https://leetcode.com/subscribe/) for the ...
Computer brand, model, and the brands and model numbers of any additional hardware. d. Operating system and version number. (The version number can be determined by typing VER at the MSDOS prompt...
Windows 98 and ME are not supported in BurnInTest version 5.3 and above. Use a version of BurnInTest prior to 5.2 for compatibility with W98 and ME. Windows 95 and Windows NT =======================...
// 2003-MAR-14 - Thanks to Jakko Van Hunen for pointing out a copy-and-paste // bug in one of the overloads of FmtArg. // // 2003-MAR-10 - Thanks to Ronny Schulz for (twice!) sending me some changes /...
-alpha option on, activate, off, deactivate, set, opaque, copy transparent, extract, background, or shape -annotate geometry text annotate the image with text -auto-gamma automagically adjust ...
copy all parts of an object. 3. 资源管理 resource management 条款13:以对象管理资源 use objects to manage resources. 条款14:在资源管理类中小心coping行为 think carefully about copying behavior in ...
• FAT16, FAT32, NTFS, Linux Ext2, Ext3, ReiserFS, and Linux SWAP partition support. Special sector-by-sector support for copying and moving partitions of any other type. Key features • New! Split ...
• FAT16, FAT32, NTFS, Linux Ext2, Ext3, ReiserFS, and Linux SWAP partition support. Special sector-by-sector support for copying and moving partitions of any other type. Key features • New! Split ...
5.3.5 Using Copy-on-Write and Snapshots 122 5.3.6 Using File Compression 124 5.3.7 Measuring Performance 124 5.3.8 Expanding a Pool 125 5.3.9 Checking a Pool 126 5.3.10 Replacing a Disk 127 5.4 NFS ...
The trees are managed, and branches are grafted as necessary, so that navigating down the tree to find a value and locate a specific record takes only a few page accesses. Because the trees are ...
Copy List with Random Pointer Linked List Cycle Linked List Cycle II Reorder List LRU Cache Palindrome Linked List 字符串 Valid Palindrome Implement strStr() String to Integer (atoi) Add Binary ...
Manage Temporary Files and Swap Space Section 22. Recreate a Directory Structure Using mtree Section 23. Ghosting Systems Chapter 3. The Boot and Login Environments Introduction ...
+ The position and size of the main window is saved on exit and restored the next time CurveExpert runs Bug Fixes: + Whoops! The new file reading procedure masked out all minus and minus signs ...
logical_and, logical_or, logical_not 7.6 证同(identity)、选择(select)、投射(project) 423 identity, select1st, select2nd, project1st, project2nd 第8章 配接器(adapter) 425 8.1 配接器之概观与...
logical_and, logical_or, logical_not 7.6 证同(identity)、选择(select)、投射(project) 423 identity, select1st, select2nd, project1st, project2nd 第8章 配接器(adapter) 425 8.1 配接器之概观与...
命令英文原意:copy 命令所在路径:/bin/cp 执行权限:所有用户 语法:cp -R [源文件或目录] [ 目的目录] -R 复制目录 -p 复制后的文件的时间和原文件时间一样 功能描述:复制文件或目录 范例: $ cp file1...
SYNONYMS substitute, alternate, swap, switch HINT This command is available in history modeling mode. HELP DSN_replace BITMAP replace_feature POPUP_LABEL Repl&ace... POPUP_MESSAGE Replaces the ...
, {r3-r10} /* copy from source address [r0] */ 197 stmiar1!, {r3-r10} /* copy to target address [r1] */ 198 cmp r0, r2 /* until source end addreee [r2] */ 199 ble copy_loop 200 #endif /* CONFIG_...