草庐IT

auto_array_ptr

全部标签

C++11 - 单独 move 数组(原始数组、std::array、std::vector)的每个元素?

在C++11中,move语义等等,人们可能想知道实际上可以move什么。这方面的一个例子是数组。是否可以move原始数组的每个元素,intarray1[8];intarray2[8];array1[0]=std::move(array2[0]);std::数组,std::arrayarray1;std::arrayarray2;array1[0]=std::move(array2[0]);和std::vectorsstd::vectorarray1;std::vectorarray2;array1[0]=std::move(array2[0]);个人? 最佳

c++ - QMap 和 std::unique_ptr

我试图防止裸指针,防止内存泄漏等。我还想将int映射到INuiSensor*.由于我也在使用Qt,所以我尝试使用QMap>来执行此操作,但QMap的源代码使这变得不可能:templateQ_INLINE_TEMPLATEtypenameQMap::iteratorQMap::insert(constKey&akey,constT&avalue){detach();Node*n=d->root();Node*y=d->end();Node*last=0;boolleft=true;while(n){y=n;if(!qMapLessThanKey(n->key,akey)){last=n;

C++ ptr_fun 找不到 void 参数化函数

我尝试使用std::ptr_fun来包装我的函数,但是当我尝试使用void参数和bool返回类型来包装一个函数时,我得到了一个错误:代码:std::functioncr=std::not1(std::ptr_fun(&funct1));功能:boolfunct1(){returnfalse;}错误:error:nomatchingfunctionforcallto'ptr_fun(bool(*)())'但每当我将参数更改为int时,问题似乎就消失了。如何使用void参数包装函数? 最佳答案 std::ptr_fun仅适用于一元函数:

c++ - OpenCV 错误 : Sizes of input arguments do not match (The operation is neither 'array op array' )

我正在做一个在树莓派上使用opencv的项目。我遇到了一个看起来很简单的障碍,但我无法解决问题。首先,这是我的代码的一部分:{gray=cvarrToMat(py);///cvShowImage("camcvWin",py);//displayonlygraychannelif(img_num%2==1){cv::imwrite("/home/pi/test/Gray_2Image1.jpg",gray);}elseif(img_num%2==0){cv::imwrite("/home/pi/test/Gray_2Image2.jpg",gray);cv::Matimg2=cv::im

c++ - Visual Studio 2013 C++ - 将 std::unique_ptr 传递给绑定(bind)函数

使用VisualStudio2013RC和C++,我尝试将std::unique_ptr传递给已使用std::bind绑定(bind)的函数。但是,我遇到了麻烦,因为当我尝试这个时VS似乎不喜欢它。这是我要编译的内容:#include#include#includevoidfunc(std::unique_ptrarg){std::cout)>bound=std::bind(&func,std::placeholders::_1);std::unique_ptrptr(newint(42));bound(std::move(ptr));return0;}这可以在GCC4.8.1中编译,

c++ - 试图理解 auto_ptr

我试图了解有关auto_ptr类如何工作的某些细节。假设您有以下类(class)(我是在一个网站上找到的,该网站上有人解释了赋值运算符的要点)。classTFoo:publicTSuperFoo{auto_ptrfBar1;auto_ptrfBar2;public:TFoo&TFoo::operator=(constTFoo&that);//variousothermethoddefinitionsgohere...}现在是赋值运算符的实现。TFoo&TFoo::operator=(constTFoo&that){if(this!=&that){auto_ptrbar1=newTBar

c++ - 使用 std::unique_ptr 管理 COM 对象

我正在尝试使用智能指针在我的类中保存COM对象,同时避免使用ComPtr。是否可以为此目的使用unique_ptr?我对智能指针很陌生,到目前为止我有点困惑。请考虑以下简化代码:classTexture{private:structComDeleter{operator()(IUnknown*p){p.Release();deletep;}}ID3D11Texture*m_dumbTexture;std::unique_ptrm_smartTexture;public:ID3D11Texture*getDumbTexture()const{returnm_dumbTexture;}ID

c++ - union 中的 std::shared_ptr

我正在实现一个“变体”类,它必须具有尽可能小的内存占用并使用共享指针机制存储一些对象。为此,我想在所有变量类型的类中建立一个union。这包括一些shared_ptr。operator=和复制构造函数必须更改变量的数据类型,从而切换到union中的另一个成员。切换到shared_ptr后,应将其重置为null而无需删除/取消拥有指针。有办法做到这一点吗?当然,还有其他方法可以实现这一点,但在我的例子中,它们通常更复杂、更不安全或消耗更多内存。不过欢迎提出任何建议。谢谢! 最佳答案 重置为null是不够的;的实现std::shared

c++ - 为什么不能形成对 'decltype(auto)' 的引用

intmain(){decltype(auto)&&a=100;}以上代码,在GCC和Clang中出错。intmain(){decltype(int)&&a=100;}此代码正确。在N4296中,在§8.3.2/6Ifatypedef(7.1.3),atypetemplate-parameter(14.3.1),oradecltype-specifier(7.1.6.2)denotesatypeTRthatisareferencetoatypeT,anattempttocreatethetype“lvaluereferencetocvTR”createsthetype“lvaluere

c++ - 使用 unique_ptr 成员编写移动构造函数的正确方法(崩溃)

以下代码在VisualStudio2013下会崩溃我想知道为什么:在这种情况下编写移动构造函数的正确方法是什么?删除移动构造函数解决了这个问题。是VC++的错误还是这段代码有误?移动构造函数的默认定义有何不同,这使得这段代码不会崩溃,而我自己的定义会崩溃?#include#includeclassA{};classFoo{public:Foo(std::unique_ptrref):mRef(std::move(ref)){}Foo(Foo&&other):mRef(std::move(other.mRef)){}Foo(constFoo&other){}Foo&operator=(c