草庐IT

unshared_copy

全部标签

c++ - 在基类和派生类中 copy-and-swap

我最近读到copy&swap现在我正在尝试在基类和派生类中实现ctors。我的基类和派生类中都有四个构造函数,但是我不确定如何实现派生类的赋值运算符。explicitBase(inti):m_i{i}{}Base(constBase&other):m_i{other.m_i}Base(Base&&other):Base(0){swap(*this,other);}Base&operator=(Baseother){swap(*this,other);return*this;}friendvoidswap(Base&a,Base&b)noexcept{usingstd::swap;swa

c++ - 通过 copy 和 decltype 捕获 Lambda 引用

考虑简单的程序:inti=0;int&j=i;autolambda=[=]{std::cout根据[expr.prim.lambda],闭包成员变量j的类型应该是int:Anentityiscapturedbycopyifitisimplicitlycapturedandthecapture-defaultis=orifitisexplicitlycapturedwithacapturethatisnotoftheform&identifieror&identifierinitializer.Foreachentitycapturedbycopy,anunnamednon-static

c++ - std::reverse_copy "error: function call has aggregate value"

#include#include#include#includeusingnamespacestd;intmain(){intarrA[]={1,2,3,4,5,6,7,8,9};vectorvecIntA(arrA,arrA+sizeof(arrA)/sizeof(arrA[0]));vectorvecIntB(vecIntA.size());//copy((vecIntA.rbegin()+3).base(),(vecIntA.rbegin()+1).base(),vecIntB.begin());//OKvector::iterators=(vecIntA.rbegin()+3)

c++ - 错误 : definition of implicitly declared copy constructor

我目前正在处理的QtC++项目有问题。这是我要介绍的一个新部分,但我发现它有点令人困惑。我创建了一些由股票、债券和储蓄类继承的Assets类。这一切都很好。然后我创建了一个名为AssetList的类,它派生了QList,这个类是我发现问题的地方。这是我目前的代码。资源列表.h#ifndefASSET_LIST_H#defineASSET_LIST_H#include"Asset.h"#includeclassAssetList:publicQList{public:AssetList(){}~AssetList();booladdAsset(Asset*);Asset*findAsse

c++ - MI 和隐式复制构造函数错误(原为 : Under what conditions can a template be the copy constructor?)

我很确定这个问题的答案是,“模板永远不可能成为复制构造函数。”不幸的是,我只花了3个小时弄清楚为什么我会收到有关递归的警告,跟踪它到复制构造函数,看着调试器发疯,不让我看递归代码,最后跟踪到一个基础构造函数中缺少“&”。你看,我有一个复杂的基于策略的设计主机,它已经运行了一段时间了。我着手将两个策略合二为一并遇到了一个递归复制构造函数。将其缩小为一个策略,该策略需要提供一个构造函数,该构造函数可以采用一种XXX概念作为其参数,但在这种情况下,我只是放弃它。所以我写了structmy_policy{templatemy_polity(Tconst){}//missing'&'...oop

c++ - QFile::copy create 会创建文件的拷贝或将内容从一个文件移动到另一个文件吗?

我正在尝试使用C++/Qt将文件从一个位置复制到另一个位置(在设备中)我试过QFile::copy("path1/file","path2");我想将路径1中的文件复制到路径2中。path2没有该文件。我只是想知道这是否是正确的方法,因为上面的代码似乎不起作用。另外,我应该在尝试复制之前打开一个文件吗?需要帮助! 最佳答案 如果你想将path1/file复制到具有相同文件名的path2中,你需要这样做:QFile::copy("path1/file","path2/file");复制允许您更改文件的名称。示例:QFile::copy

c++ - std::copy 用于动态分配的指针

这可能是一个非常新手的问题。我正在处理的类(我们称之为MyClass)包含以下变量intnbins;double*bins;//=newdouble[nbins+i]因为我正在实现一个复制构造函数MyClass(constMyClass&source)我需要复制source.bins。我打算使用algorithm中的std::copy,但我发现的所有示例都展示了如何复制静态数组,如doublebins[]=。..或标准数据结构,如std::vector。我找不到任何示例,其中std::copy用于像double*bins=newdouble[nbins+i]这样的指针。所以,我的问题是

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;} 最佳答案