草庐IT

copy-assignment

全部标签

C++ move 语义 : why copy assignment operator=(&) is called instead of move assignment operator=(&&)?

我有以下代码:#include#includeusingstd::cout;structSomeType{SomeType(){}SomeType(constSomeType&&other){cout我希望move构造函数调用move赋值运算符。下面是这个程序的输出:SomeType(SomeType&&)operator=(constSomeType&)operator=(SomeType&&)如您所见,move赋值运算符已成功调用,但在move构造函数内分配给*this时未成功调用。为什么会发生这种情况,我能以某种方式解决它吗? 最佳答案

c++ - 返回值优化 : ho can I avoid copy construction of huge STL containers.

当我想要一个函数返回一个容器时:vectorfunc(){vectorresult;...returnresult;}按以下方式使用:vectorresult=func();为了避免复制我的容器的开销我经常编写函数,以便它只返回接受一个容器的非常量实例。voidfunc(vector&result){result.clear();...result;}按以下方式使用:vectorresult;func(result);难道我的努力没有意义,因为我可以确定编译器总是使用返回值优化? 最佳答案 没有意义。你提到的RVO类型称为命名RVO

c++ - std::copy 和 std::vector 问题

我明白为什么这会导致段错误:#include#includeusingnamespacestd;intmain(){vectorv;intiArr[5]={1,2,3,4,5};int*p=iArr;copy(p,p+5,v.begin());return0;}但为什么这不会导致段错误?#include#includeusingnamespacestd;intmain(){vectorv;intiArr[5]={1,2,3,4,5};int*p=iArr;v.reserve(1);copy(p,p+5,v.begin());return0;} 最佳答案

c++ - 使用 std::ifstream、std::istream_iterator 和 std::copy 不读取整个文件

我在一个188字节的文件中使用了以下代码:std::ifstreamis("filename",std::ios::binary);std::vectorbuffer;std::istream_iteratori_input(is);std::copy(i_input,std::istream_iterator(),std::back_inserter(buffer));std::cout但是它只读取了188个字节中的186个字节。我已经在十六进制编辑器和ls-al中确认了文件大小。 最佳答案 我不知道为什么,但默认情况下似乎会跳过

c++ - 错误 :incompatible types in assignment of 'const char[5]' to 'char[10]'

我已经将c定义为charc[][10]在函数定义中并像c[i]="gray";一样使用它怎么了?我在网上搜索,它显示相同的语法。谢谢。 最佳答案 您不能对数组使用赋值(=)。如果将c更改为指针数组,这可能会起作用,具体取决于您需要使用它做什么。constchar*c[20];c[i]="gray";或者如果声明的类型必须是数组的数组,您可以使用strncpy:charc[20][10];strncpy(c[i],"gray",sizeof(c[i])); 关于c++-错误:incompa

c++ - QFile::copy() 的进度条?

我正在制作一个在Qt中复制文件的程序。我想知道如何将QProgressBar与boolQFile::copy(constQString&fileName,constQString&newName)一起使用。这甚至可以通过copy函数实现吗?可以暂停复制过程吗? 最佳答案 你不能使用静态QFile::copy()方法来做到这一点。正如Maciej所说,您需要编写自己的类(class)。它应该使用两个QFile对象,一个用于读取,一个用于写入。分部分传输数据(例如整个文件大小的1%)并在每个部分后发出进度信号。您可以将此信号连接到进度对

c++ - 为什么 std::is_copy_constructible 的行为不如预期?

#includeintmain(){std::is_constructible_v;//false,asexpected.std::is_copy_constructible_v;//true,NOTasexpected!}根据cppref:IfTisanobjectorreferencetypeandthevariabledefinitionTobj(std::declval()...);iswell-formed,providesthememberconstantvalueequaltotrue.Inallothercases,valueisfalse.std::is_copy_c

c++ - 使用 boost::assign::list_of 构造 std::vector 时的歧义

这段代码:std::vector(boost::assign::list_of(1)(2)(3));给出错误:main.cpp:Inmemberfunction'void::RequestHandler::processRequest(Foo&,Bar,unsignedint,unsignedint*,constchar*,boost::shared_ptr&)':main.cpp:450:error:callofoverloaded'vector(boost::assign_detail::generic_list&)'isambiguous/4.4.2/bits/stl_vecto

c++ - STL 容器 move 语义并按值返回 : how many times of copying get avoided away?

我知道在C++11中,move语义已经在STL容器中实现以避免临时对象。人们说现在编写按值返回的函数是完美的。但我对究竟有多少次复制实际上被避免感到困惑。请看下面的例子:vectormyVector(){vectorres;res.push_back(4);res.push_back(5);returnres;}vectorv=myVector();我的理解是在c++03中,myVector返回res的拷贝(4,5复制了一次),在评估vectorv=myVector();时vector的复制构造函数vector(constvector&)被调用(4,5复制了两次)。但是在具有move语

c++ - std::vector::assign/std::vector::operator=(const&) 是否保证重用 `this` 中的缓冲区?

如果我将一个vector分配或复制到另一个vector(其容量与前者的大小相同或更大),我可以假设后者的缓冲区将被重用吗?下面的例子证明我可以,但是,标准保证吗?std::vector::assign和std::vector::operator=在这方面的行为有什么不同吗?#include#include#includeintmain(){std::vectora{1,2,3,4,5};std::vectorb{1,2,3,4};std::vectorc{1,2,3,4,5,6,7,8,9,10};std::coutLiveexample.更新:Thisanswer提到voidassi