【原创】C++之自定义高效的swap(1)
1 问题背景
namespace std{
template<typename T>
void swap(T& a, T& b) //std::swap的典型实现
{
T temp(a); //一次拷贝,两次赋值
a = b;
b = temp;
}
}
2 自定义高效的swap函数
2.1 swap成员函数
2.1.1 方法
void T::swap(T& t) noexcept;
void swap(T& a, T& b) noexcept
{
a.swap(b);
}
2.1.2 典型实现
#include <iostream>
#include <string>
class ClassTest{
public:
friend std::ostream& operator<<(std::ostream &os, const ClassTest& s);
friend void swap(ClassTest &a, ClassTest &b) noexcept;
ClassTest(std::string s = "abc") :str(new std::string(s)){} //默认构造函数
ClassTest(const ClassTest &ct) :str(new std::string(*ct.str)){} //拷贝构造函数
ClassTest &operator=(const ClassTest &ct) //拷贝赋值函数
{
str = new std::string(*ct.str);
return *this;
}
~ClassTest() //析构函数
{
delete str;
}
void swap(ClassTest &t) noexcept
{
using std::swap;
swap(str,t.str); //交换指针,而不是string数据
}
private:
std::string *str; //一个指针资源
};
std::ostream& operator<<(std::ostream &os,const ClassTest& s)
{
os << *s.str;
return os;
}
void swap(ClassTest &a, ClassTest &b) noexcept
{
a.swap(b);
}
int main(int argc, char const *argv[])
{
ClassTest ct1("ct1");
ClassTest ct2("ct2");
std::cout << ct1 << std::endl;
std::cout << ct2 << std::endl;
swap(ct1, ct2);
std::cout << ct1 << std::endl;
std::cout << ct2 << std::endl;
return ;
}
- (2)中的swap函数需要和类T位于同一的命名空间里,否则外部调用swap函数可能会解析不到
- swap函数最好使它不要抛出异常,就像移动构造函数和移动赋值函数一样。
- (2)中的函数可以声明为类T的友元函数,并且设置为内联函数
- 做真实交换的swap函数,需要使用using std::swap;
2.1.2 关于using std::swap
void swap(ClassTest &t) noexcept
{
using std::swap;
swap(str, t.str); //交换指针,而不是string数据
}
- 普通的名字查找:先在当前的作用域里查找,查找不到再到外层作用域中查找,即局部相同名字的变量或函数会隐藏外层的变量或作用域
- 实参相关的名字查找(ADL)(链接2)
- 涉及函数模板匹配规则:一个调用的候选函数(关于候选函数请参考C++ Primer第五版中关于函数的一章)包括所有模板实参推断成功的函数模板实例;候选的函数模板总是可行的,因为模板实参推断会排除任何不可行的模板;如果恰好有一个函数(或模板)比其他函数更加匹配,则选择该函数;同样好的函数里对于有多个函数模板和只有一个非模板函数,会优先选择非模板函数;同样好的函数里对于没有非模板函数,那么选择更特例化的函数模板
- 因为C++会优先在当前的作用域里查找,所以使用using std::swap将标准库的swap模板函数名字引入该局部作用域,重载当前作用域的同名函数,隐藏外层作用域的相关声明。为什么using std::swap不会隐藏外层的void swap(ClassTest &a, ClassTest &b) noexcept函数呢?参见:这里。其中说到,当经过普通的名字查找后(没有包括ADL),如果候选函数中有类成员、块作用域中的函数声明(不包括using声明引入的)、其他同名的函数对象或变量名,则不启动ADL查找了。如果没有,则进行ADL查找。因此在经过普通的查找后,发现并没有匹配的函数,最后再经过ADL找到了标准库中的swap和外层作用域的void swap(ClassTest &a, ClassTest &b) noexcept,由于后者较匹配,编译器优先选择后者。
- 如果str类型有自定义的swap函数,那么第4行代码的swap调用将会调用str类型自定义的swap函数
- 但是如果str类型并没有特定的swap函数,那么第4行代码的swap调用将会被解析到标准库的std::swap
2.2 swap友元函数
2.2.1 方法
void swap(ClassTest &a, ClassTest &b) noexcept
{
using std::swap;
//swap交换操作
}
2.2.2 典型实现
#include <iostream>
#include <string>
class ClassTest{
public:
friend std::ostream& operator<<(std::ostream &os, const ClassTest& s);
friend void swap(ClassTest &a, ClassTest &b) noexcept;
ClassTest(std::string s = "abc") :str(new std::string(s)){} //默认构造函数
ClassTest(const ClassTest &ct) :str(new std::string(*ct.str)){} //拷贝构造函数
ClassTest &operator=(const ClassTest &ct) //拷贝赋值函数
{
str = new std::string(*ct.str);
return *this;
}
~ClassTest() //析构函数
{
delete str;
}
private:
std::string *str; //一个指针资源
};
std::ostream& operator<<(std::ostream &os, const ClassTest& s)
{
os << *s.str;
return os;
}
void swap(ClassTest &a, ClassTest &b) noexcept
{
using std::swap;
swap(a.str,b.str); //交换指针,而不是string数据
}
int main(int argc, char const *argv[])
{
ClassTest ct1("ct1");
ClassTest ct2("ct2");
std::cout << ct1 << std::endl;
std::cout << ct2 << std::endl;
swap(ct1, ct2);
std::cout << ct1 << std::endl;
std::cout << ct2 << std::endl;
return ;
}
- (2)中的swap函数需要和类T位于同一的命名空间里,否则外部调用swap函数可能会解析不到
- swap函数最好使它不要抛出异常,就像移动构造函数和移动赋值函数一样
本文链接:【原创】C++之swap(1)http://www.cnblogs.com/cposture/p/4939971.html
【原创】C++之自定义高效的swap(1)的更多相关文章
- C++-高效的swap
原始版本: template<typename T> void swap(T& a, T& b) { T tmp(a); a = b; b = tmp; } 此版本不重视效 ...
- 安装Ubuntu时,遇到自定义交换空间swap大小设置问题
【整理】Ubuntu自定义分区设置 在安装Ubuntu时,如果使用的是一个新硬盘那么安装向导会建议你使用整个硬盘,如果硬盘上已经有数据了,向导会建议使用剩余的空间。不管怎样,是由向导自动划分的分区。 ...
- c++下为使用pimpl方法的类编写高效的swap函数
swap函数是c++中一个常用的函数,用于交换两对象的值,此外还用于在重载赋值运算符中处理自赋值情况和进行异常安全性编程(见下篇),标准模板库中swap的典型实现如下: namespace stl { ...
- 【原创】Android自定义适配器的使用方法
比如说我们已经得到了数据,想在一个listview或者在其他的控件中显示的,并且我们显示想要自己设计样式来显示的话就要用到自定义适配器了,下面让我们结合代码讲一下具体的使用方法: 代码会有注释的哦: ...
- 读书笔记 effective c++ Item 25 实现一个不抛出异常的swap
1. swap如此重要 Swap是一个非常有趣的函数,最初作为STL的一部分来介绍,它已然变成了异常安全编程的中流砥柱(Item 29),也是在拷贝中应对自我赋值的一种普通机制(Item 11).Sw ...
- VSCode 必装的 10 个高效开发插件
本文介绍了目前前端开发最受欢迎的开发工具 VSCode 必装的 10 个开发插件,用于大大提高软件开发的效率. VSCode 的基本使用可以参考我的原创视频教程「VSCode 高效开发必装插件」. V ...
- VSCode高效开发插件
VSCode 必装的 10 个高效开发插件 https://www.cnblogs.com/parry/p/vscode_top_ten_plugins.html 本文介绍了目前前端开发最受欢迎的开发 ...
- swap function & copy-and-swap idiom
在C++中,众所周知在一个资源管理类(例如含有指向堆内存的指针)中需要重新定义拷贝构造函数.赋值运算符以及析构函数(Big Three),在新标准下还可能需要定义移动构造函数和移动赋值运算符(Big ...
- [Swift]LeetCode24. 两两交换链表中的节点 | Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. Example: Given 1->2->3 ...
随机推荐
- JSOI2018 简要题解
潜入行动 复杂度分析题. 定义状态fi,j,0/1,0/1f_{i,j,0/1,0/1}fi,j,0/1,0/1表示以iii为根子树放jjj个机器iii这个放不放,iii这个是否已放来进行dpdpd ...
- 解决更新ssh后在/etc/init.d下无sshd的问题
1.将远程服务器的/etc/init.d/ssd 文件拷贝到本地 scp /etc/init.d/ssh root@IP地址:/etc/init.d 2.vi /etc/init.d/sshd 3 ...
- 连接EMC存储系统
1.准备一台笔记本电脑,一根网线即可. 2.将网线一头连接笔记本电脑,另一头连接存储.(连接存储的一头应连接到有扳手图标的那一网口上) 3.配置IP地址 IP:128.221.1.254 子网掩码:2 ...
- 微信小程序拉起登录的操作
第一步,前端调用wx.login()接口把token数据请求过来, 第二部,把tok嗯发送到总计的服务器,然后进行微信openid和assession的获取 第三部验证session是否过期,过期重新 ...
- EFLinq查询
1.无参数查询var model = db.Database.SqlQuery<UserInfo>("select* from UserInfoes ").ToList ...
- 第二次OO总结
作业5——多线程电梯 好像失忆了,竟然对这三部电梯很陌生,我尽量回忆一下当时挣扎的场景orz 整体思路和第二次电梯差不多,但是将调度器类套在了电梯类里 优点可能是没有无效,足矣!!!缺点emmmm要是 ...
- RK3288 uboot启动流程
VS-RK3288嵌入式板卡 U-boot 启动流程小结 bl board_init_f -> crt0.S initcall_run_list(init_sequence_f) - ...
- unic
在线考试 答题剩余时间0小时51分18秒 考生须知 1.本次考试结束后,剩余补考次数:2次 2.考试时间为60分钟,超时系统自动交卷 3.本次考试满分100分(5*20道),60分通过考试 1. (单 ...
- Ubuntu 离线安装 docker
1.下载离线包,网址:https://download.docker.com/linux/ubuntu/dists/xenial/pool/stable/amd64/ 离线安装docker需要下载3个 ...
- Monitor类:Object synchronization method was called from an unsynchronized block of code.
最近,在维护以前老系统的时候,发现了这样一个错误:Object synchronization method was called from an unsynchronized block of co ...