move移动语义和forward完美转发
vector使用右值的拷贝构造函数,可以直接用临时对象拷贝构造,直接将资源移动过来。
这里我们只讲述vector中的push_back方法;
匹配右值的push_back函数:
一个右值引用变量它本身还是一个左值。
但是此时val是一个左值,不会匹配右值的construct函数,只会匹配左值的construct函数。
可以看到,右值的临时对象匹配的是左值的construct函数
我们想要 匹配右值的push_back函数 中的construct函数匹配 右值construct函数!
怎么办?
使用移动语义,std::move()相当于将左值强制转换成右值引用类型。
move源代码:
现在可以匹配到右值引用的CMyString的拷贝构造。
但是上面的太复杂了,模仿vector库中的实现:
引用重叠:
- 左值引用+右值引用=左值引用
- 右值引用+右值引用=右值引用
问题:
不管形参是左值引用变量还是右值引用变量,val本身都是一个左值。
这样会导致construct函数还是匹配的是左值引用的construct函数
解决方法: - 在模板函数调用construct中引入std::forward,可以返回val到底是左值还是右值
- forward:类型完美转发,能够识别左值和右值类型
- 可以根据参数val本身的定义识别出val是左值还是右值。如果val是左值类型它就会返回左值类型,调用construct的左值引用的函数。如果val是右值类型,它就返回右值类型,调用construct的右值引用的函数。
运行结果:
没有问题!
运用这种思想,可以把construct函数也修改下:
运行:没有问题
- forward是通过模板的非完全特例化实现的。
- move(左值):移动语义,得到右值类型 (int&&)a
#include <iostream>
#include <vector>
using namespace std;
template<typename T>
struct Allocator
{
T* allocate(size_t size)//负责内存开辟
{
return (T*)malloc(sizeof(T) * size);
}
void deallocate(void *p)//负责内存释放
{
free(p);
}
/*void construct(T *p, const T &val)//负责对象构造
{
new (p) T(val); // 定位new
}
void construct(T *p, T &&val)//负责责对象构造
{
new (p) T(std::move(val));
//定位new 强转成右值引用类型 调用底层的CMyString的右值引用的拷贝构造函数
}*/
//可以用std::move强转成右值,也可以如下做法:
//无法区分右值和左值的简便的解决办法:
template<typename Ty>
void construct(T *p, Ty &&val)
{
new (p) T(std::forward<Ty>(val));
}
void destroy(T *p)//负责对象析构
{
p->~T();// ~T()代表了T类型的析构函数
}
};
/*
容器底层内存开辟,内存释放,对象构造和析构,都通过allocator空间配置器来实现
*/
template<typename T, typename Alloc = Allocator<T>>
class vector
{
public:
vector(int size = 10)//构造函数
{
//需要把内存开辟和对象构造分开处理
//_first = new T[size];
_first = _allocator.allocate(size);
_last = _first;
_end = _first + size;
}
~vector()//析构函数
{
//析构容器有效的元素,然后释放_first指针指向的堆内存
//delete[]_first;
for (T *p = _first; p != _last; ++p)
{
_allocator.destroy(p);//把_first指针指向的数组的有效元素进行析构操作
}
_allocator.deallocate(_first);//释放堆上的数组内存
_first = _last = _end = nullptr;
}
vector(const vector<T> &rhs)//拷贝构造函数
{
int size = rhs._end - rhs._first;
//_first = new T[size];
_first = _allocator.allocate(size);
int len = rhs._last - rhs._first;
for (int i = 0; i < len; ++i)
{
//_first[i] = rhs._first[i];
_allocator.construct(_first + i, rhs._first[i]);
}
_last = _first + len;
_end = _first + size;
}
vector<T>& operator=(const vector<T> &rhs)//赋值函数
{
if (this == &rhs)
return *this;
//delete[]_first;
for (T *p = _first; p != _last; ++p)
{
_allocator.destroy(p);//把_first指针指向的数组的有效元素进行析构操作
}
_allocator.deallocate(_first);
int size = rhs._end - rhs._first;
//_first = new T[size];
_first = _allocator.allocate(size);
int len = rhs._last - rhs._first;
for (int i = 0; i < len; ++i)
{
//_first[i] = rhs._first[i];
_allocator.construct(_first + i, rhs._first[i]);
}
_last = _first + len;
_end = _first + size;
return *this;
}
void pop_back()//从容器末尾删除元素
{
if (empty())
return;
//--_last; //不仅要把_last指针--,还需要析构删除的元素
--_last;
_allocator.destroy(_last);
}
T back()const//返回容器末尾的元素的值
{
return *(_last - 1);
}
bool full()const { return _last == _end; }
bool empty()const { return _first == _last; }
int size()const { return _last - _first; }
//
/*void push_back(const T &val)//接收左值
{
if (full())
expand();
_allocator.construct(_last, val);
_last++;
}
void push_back(T &&val)//接收右值 一个右值引用变量本身还是一个左值
{
if (full())
expand();
_allocator.construct(_last, std::move(val));
//val本身还是左值啊。匹配的还是左值的construct。怎么办?
//使用std::move把val 强转成右值引用类型
_last++;
}*/
//void push_back(CMyString &val)
//CMyString&& + && = void push_back(CMyString&&val)
template<typename Ty>//函数模板的类型推演 + 引用折叠
void push_back(Ty &&val)//Ty CMyString& + && = CMyString&
{
if (full())
expand();
//move(左值):移动语义,得到右值类型 (int&&)a
//forward:类型完美转发,能够识别左值和右值类型
_allocator.construct(_last, std::forward<Ty>(val));
_last++;
}
private:
T *_first;//指向数组起始的位置
T *_last;//指向数组中有效元素的后继位置
T *_end;//指向数组空间的后继位置
Alloc _allocator; // 定义容器的空间配置器对象
void expand()//容器的二倍扩容
{
int size = _end - _first;
//T *ptmp = new T[2 * size];
T *ptmp = _allocator.allocate(2 * size);
for (int i = 0; i < size; ++i)
{
//ptmp[i] = _first[i];
_allocator.construct(ptmp + i, _first[i]);
}
//delete[]_first;
for (T *p = _first; p != _last; ++p)
{
_allocator.destroy(p);
}
_allocator.deallocate(_first);
_first = ptmp;
_last = _first + size;
_end = _first + 2 * size;
}
};
int main()
{
CMyString str1 = "aaa";
vector<CMyString> vec;
cout << "-----------------------" << endl;
vec.push_back(std::move(str1));//CMyString&
vec.push_back(CMyString("bbb"));//CMyString&& move forword
cout << "-----------------------" << endl;
return 0;
}