草庐IT

unique-values

全部标签

c++ - 在没有 std::move 的情况下如何按值返回 unique_ptr?

这个问题在这里已经有了答案:Returningunique_ptrfromfunctions(7个答案)关闭8年前。std::unique_ptrptr(){std::unique_ptrp(newint(3));returnp;//Whydoesn'tthisrequireexplicitmoveusingstd::move?}//Whydidn'tthedatapointedtoby'p'isnotdestroyedherethoughpisnotmoved?intmain(){std::unique_ptra=ptr();//Whydoesn'tthisrequirestd::m

c++ - boost 序列化 std::unique_ptr 支持

boost序列化库支持std::unique_ptr的序列化吗?我试图编译下面的代码,但如果我包含boost::archive::text_oarchiveoa(ofs);oa行,编译器(顺便说一句,带有-std=c++11标志的gcc4.7)抛出一个错误/usr/include/boost/serialization/access.hpp:118:9:错误:‘classstd::unique_ptr’没有名为‘serialize’的成员#include#include#include#include#includeclassMyDegrees{public:voidsetDeg(in

c++ - 尽管使用 unique_ptr 内存泄漏

我有一个类的构造函数,它用传递给它的值初始化该类内部的unique_ptr。出于某种原因,valgrind提示内存泄漏:22,080(24direct,22,056indirect)bytesin1blocksaredefinitelylostinlossrecord6of6at0x4C2C7A7:operatornew(unsignedlong)(in/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)by0x4A64FB:VectorBasedNodeOrder::VectorBasedNodeOrder(VectorBasedN

c++ - 如果我按值(value)捕获但不使用它,我还能得到一份拷贝吗?

我不想在lambda中按值捕获shared_ptr以确保对象的生命周期延长到调用lambda函数的时间点。否则我实际上不需要shared_ptr。如果我这样做:shared_ptrsp;sp->async_call([sp](){});sp是否保证被复制,即使正文没有引用它? 最佳答案 我会说这是有保证的,因为这句话。5.1.221Whenthelambda-expressionisevaluated,theentitiesthatarecapturedbycopyareusedtodirect-initializeeachcorr

c++ - '_ITERATOR_DEBUG_LEVEL' : value '0' doesn't match value '2' 检测到不匹配

使用VS2010构建时,我正在构建一个导致许多此类链接错误的库:errorLNK2038:mismatchdetectedfor'_ITERATOR_DEBUG_LEVEL':value'0'doesn'tmatchvalue'2'导致我必须同时发布我的库的发行版和调试版。我没有理由发布lib的调试版本,它只会使二进制分发版膨胀。但是在调试中构建的客户端代码拒绝链接到我的发布库。我以前见过这个问题,但他们似乎没有问正确的问题。我明白这个错误是什么,以及为什么我会得到它(好吧,有点;我不确定到底是什么发出了依赖性。你呢?),但我想知道的是如何消除这种依赖性的发生在我的库中?类似于在使用冲

c++ - 在 map 中使用 unique_ptr 时删除 std::pair 中的函数

我有一段C++代码,我不确定它是否正确。考虑以下代码。#include#include#includeusingnamespacestd;intmain(intargc,char*argv[]){vector>>v;v.resize(5);returnEXIT_SUCCESS;}GCC编译这段代码没有问题。然而,英特尔编译器(版本19)因错误而停止:/usr/local/[...]/include/c++/7.3.0/ext/new_allocator.h(136):error:function"std::pair::pair(conststd::pair&)[with_T1=cons

c++ - 在 Google 测试失败输出消息中自定义实际/预期的 "Value of"字符串

我从GoogleTest得到以下输出单元测试:UnitTests.cc:56:FailureValueof:LineSegment2i(Vector2i(-10,0),Vector2i(-10,10)).toLine()Actual:24-byteobjectExpected:Line(10,3.14159265358979323846)Whichis:24-byteobject[FAILED]LineSegmentTests.toLine(1ms)那个十六进制输出字符串不是很有用。有什么我可以添加到Line的吗?类(相等性测试失败)在这种情况下提供更多有用的错误?有问题的类已经覆盖了

c++ - `unique_ptr` 上的原子操作

std::shared_ptrhasspecializationsforatomicoperations像atomic_compare_exchange_weak和family,但我找不到关于std::unique_ptr的等效特化的文档。有没有?如果不是,为什么不呢? 最佳答案 可以提供std::shared_ptr的原子实例的原因并且不可能为std::unique_ptr这样做在他们的签名中暗示。比较:std::shared_ptr对比std::unique_ptr其中D是删除器的类型。std::shared_ptr需要分配一个

c++ - std::move 和 unique_ptr::reset 之间有什么区别?

对于std::unique_ptr的p1和p2,std::move()有什么区别>和std::unique_ptr::reset()?p1=std::move(p2);p1.reset(p2.release()); 最佳答案 根据[unique.ptr.single.assign]/2中移动分配的标准规范,答案应该是显而易见的:Effects:Transfersownershipfromuto*thisasifbycallingreset(u.release())followedbyanassignmentfromstd::forw

c++ - 什么是 "a value not associated with an object"?

C++11和C++14标准(以及工作草案)在§3.10.1中说:Aprvalue(“pure”rvalue)isanrvaluethatisnotanxvalue.[Example:Theresultofcallingafunctionwhosereturntypeisnotareferenceisaprvalue.Thevalueofaliteralsuchas12,7.3e5,ortrueisalsoaprvalue.—endexample]和Anrvalue(socalled,historically,becauservaluescouldappearontheright-han