对于unique_ptr这样的类型来说,正好move构造完原始对象就被置空了,只是因为这样实现比较方便而已。
直接换给str1就行。而给str1置空本身需要初始化还需要给str2销毁,使得move具备了额外的代价,不符合...
1#include <iostream>2#include <vector>3#include <memory>4#include <thread>5#include <type_traits>6#include <typeinfo>7#include <sstream>8#include <utility>91011classStrVec12{13friend std::ostream &operator<<(std::ostream &os,constStrVec &rhs);1415private:16std::string*elemnets_;17std::...
对于unique_ptr这样的类型来说,正好move构造完原始对象就被置空了,只是因为这样实现比较方便而已。
std::string_view str3{std::move(str1)};就相当于:std::string_view str3{str1};相当于执行...
不学术一点,现实一点的说法就是std::string_view不是个句柄类,因此move等价于copy。实际上从这点可以...
首先,你要明白 move 的目的是什么:转移资源所属权,比如
但是string_view显然没有这样的需求,它本身不占有资源。那复制就好了,交换还是置空徒增开销。
string_view是一个简单的 view,它本身不拥有资源。就跟普通的 int64_t 一样地不拥有资源。那么,你 ...
我们来看问题中的代码:std::string_viewstr1{"test01"};std::string_viewstr2{"test02"};std::...