草庐IT

unique_constraint

全部标签

c++ - unique_ptr 的 deque vector 的编译器错误

以下代码无法在gcc5.3上编译,编译器错误提示unique_ptr的复制构造函数以某种方式被调用。有人可以解释为什么会这样吗?#include#include#includeusingFoo=std::deque>;voidfoo(){std::vectora;a.emplace_back();//thisfailstocompile}编译错误中的关键行是:gcc-4.9.2/include/c++/4.9.2/bits/stl_construct.h:75:7:error:useofdeletedfunction‘std::unique_ptr::unique_ptr(consts

c++ - 将 unique_ptr<Object> 作为 unique_ptr<const Object> 返回

我有这样的方法:std::unique_ptrTable::GetStats()const{std::unique_ptrresult;//...//Preparestats.Undersomeconditionsanexceptionmaybethrown.//...returnresult;}问题是它无法编译:error:cannotbind‘std::unique_ptr’lvalueto‘std::unique_ptr&&’我可以使用以下绕过方法使其编译:returnstd::unique_ptr(result.release());不过好像有点过分了。我无法理解,从C++的角

C++, Linux : error: conversion from ‘boost::unique_future<void>’ to non-scalar type ‘boost::shared_future<void>’ requested. 如何绕过它?

我尝试使用boostthreadfutures.所以如图here我们可以得到sharedfuture来自packagedtask.所以我在linux上尝试这样的功能:templatevoidpool_item(boost::shared_ptr>pt){boost::shared_futurefi=pt->get_future();//error//...但调用它时出错:../../src/cf-util/thread_pool.h:Inmemberfunction‘voidthread_pool::pool_item(boost::shared_ptr>)[withtask_retu

c++ - std::vector<std::unique_ptr<T>> 有更好的替代方案吗?

我正在寻找需要满足以下要求的容器(针对游戏开发,尤其是实体管理):快速迭代没有存储元素的拷贝不会使指向元素的指针失效删除和插入元素例子:Containercontainer;//ThispointerwillalwayspointtotheplayerEntity*player{newEntity};container.add(player);//Setsomeentitiesto"dead"for(auto&e:container)if(e->type=="Enemy")e->die();//Useerase-removeidiomon"dead"entitiescontainer.

c++ - 使用包含 std::unique_ptr 的结构的 std::vector 声明类时出错

虽然我已经使用C#工作了几年,但用C++完成工作有时对我来说仍然很困难。我完全接受智能指针的使用,但现在我面临以下难题我有一个结构Foo,例如structFoo{Foo(std::unique_ptrbar):m_myBar(std::move(bar)){}private:std::unique_ptrm_myBar;};在另一个类中,我想要一个包含Foo实例的vector,但是下面这行std::vectorm_Foos;产生编译错误,指出拷贝构造函数被删除。在SO线程“WhycanInotpush_backaunique_ptrintoavector?”中给出了解释和补救措施。但是

c++ - unique_ptr 和 OpenSSL 的 STACK_OF(X509)*

我使用一些using语句和unique_ptr来与OpenSSL一起工作,如suggestedinanotherquestion.否则,代码会变得非常丑陋,而且我不太喜欢goto语句。到目前为止,我已经尽可能地更改了我的代码。以下是我使用的示例:usingBIO_ptr=std::unique_ptr;usingX509_ptr=std::unique_ptr;usingEVP_PKEY_ptr=std::unique_ptr;usingPKCS7_ptr=std::unique_ptr;...BIO_ptrtbio(BIO_new_file(some_filename,"r"),::

c++ - 将 std::unique_ptr 传递给构造函数以获取所有权

我想将std::unique_ptr传递给一个类的构造函数,该类将获得std::unique_ptr所拥有的数据的所有权。下面的方法foo和bar在编译器处理它们的方式方面是否有任何差异,从而使其中之一更可取?foo类:templateclassfoo{std::unique_ptrdata_;public:foo(std::unique_ptr&&data):data_{std::forward>(data)}{}};bar类:templateclassbar{std::unique_ptrdata_;public:bar(std::unique_ptrdata):data_{std

c++ - 混合 boost::optional 和 std::unique_ptr

我承认:我爱上了可选的概念。自从我发现它以来,我的代码质量有了很大的提高。明确变量是否有效比简单的错误代码和带内信号要好得多。它还让我不必担心必须阅读文档中的契约(Contract),或者担心它是否是最新的:代码本身就是契约(Contract)。就是说,有时我需要处理std::unique_ptr。这种类型的对象可能为空,也可能不是;在代码中的给定点不可能知道std::unique_ptr是否应该有值;不可能从代码中知道契约。我想以某种方式混合optional(可能与boost::optional)和std::unique_ptr,这样我就有一个动态分配的对象,具有范围破坏和适当的复制

c++ - 将 unique_ptr 数组移动到另一个数组的正确方法

我有一个包含在std::unique_ptr中的数组,我想将内容移动到另一个相同类型的数组中。我是否需要编写一个循环来一个一个地移动元素,或者我可以使用类似std::move的东西吗?constintlength=10;std::unique_ptrdata(newint[length]);//Initialize'data'std::unique_ptrnewData(newint[length]);//Fill'newData'withthecontentsof'data'编辑:另外,如果数组大小不同怎么办? 最佳答案 给定的目

c++ - 为什么 std::condition_variable 采用 unique_lock 而不是 lock_guard?

这个问题在这里已经有了答案:C++11:whydoesstd::condition_variableusestd::unique_lock?(2个答案)关闭4年前。std::condition_variable使用如下:std::condition_variablecv;...std::unique_locklk(m);cv.wait(lk,[]{returnprocessed;});在我看来有一个有趣的问题。unique_lock可以延迟,它可以被交换掉。它可能有许多其他代码设计原因,不一定是错误的,它实际上没有被锁定。例如。std::unique_locklk(m,std::try