草庐IT

exception_ptr

全部标签

c++ - 将 std::vector< std::unique_ptr< int>> 的所有权转移到正在构造的类的正确方法

转让std::vector>所有权的正确方法是什么?到正在构建的类?下面是我想要做的代码表示。我意识到无论是通过值还是通过引用将vector传递给构造函数,它都是不正确的(不会编译)并且违反了“唯一性”。我希望Foo成为vector的新所有者,并希望调用函数放弃所有权。我需要构造函数来获取std::unique_ptr>>这样做?Foo.hclassFoo{public:Foo(vector>vecOfIntPtrsOwnedByCaller);private:vector>_vecOfIntPtrsOwnedByFoo;}Foo.cppFoo::Foo(std::vector>vec

c++ - 将带有自定义删除器的 unique_ptr 移动到 shared_ptr

我有一个函数可以创建一个带有自定义删除器的unique_ptr并返回它:autogive_unique_ptr(){autodeleter=[](int*pi){deletepi;};int*i=newint{1234};returnstd::unique_ptr(i,deleter);}在该函数的客户端代码中,我想将unique_ptr移动到shared_ptr中,但鉴于我不知道该怎么做在函数之外不知道我的自定义删除器的decltype。我猜它应该是这样的:autouniquePtr=give_unique_ptr();autosharedPtr=std::shared_ptr(st

c++ - 将带有自定义删除器的 unique_ptr 移动到 shared_ptr

我有一个函数可以创建一个带有自定义删除器的unique_ptr并返回它:autogive_unique_ptr(){autodeleter=[](int*pi){deletepi;};int*i=newint{1234};returnstd::unique_ptr(i,deleter);}在该函数的客户端代码中,我想将unique_ptr移动到shared_ptr中,但鉴于我不知道该怎么做在函数之外不知道我的自定义删除器的decltype。我猜它应该是这样的:autouniquePtr=give_unique_ptr();autosharedPtr=std::shared_ptr(st

c++ - std::thread - "terminate called without an active exception",不想 'join' 它

根据ThisQuestion,我正在使用线程来终止用户输入的函数。我的代码看起来像:boolstopper=false;threadstopThread(userStop,&stopper);//startthreadlookingforuserinputfor(inti=0;i在哪里,userStop(bool*st){charchChar=getchar();if(chChar=='\n'){*st=true;}}当我运行它时,我收到错误terminatecalledwithoutanactiveexception。基于这些问题:threadterminatecalledwitho

c++ - std::thread - "terminate called without an active exception",不想 'join' 它

根据ThisQuestion,我正在使用线程来终止用户输入的函数。我的代码看起来像:boolstopper=false;threadstopThread(userStop,&stopper);//startthreadlookingforuserinputfor(inti=0;i在哪里,userStop(bool*st){charchChar=getchar();if(chChar=='\n'){*st=true;}}当我运行它时,我收到错误terminatecalledwithoutanactiveexception。基于这些问题:threadterminatecalledwitho

c++ - shared_ptr 与非指针资源

在C++11中是否可以使用shared_ptr来控制非指针资源?可以使用unique_ptr来管理非指针资源。这是通过实现一个自定义删除器类来完成的,该类提供:一个typedef{TYPE}指针;其中{TYPE}是非指针资源类型operator()(pointer)释放受控资源...然后使用自定义删除器作为第二个模板参数实例化一个unique_ptr。例如,在Windows下,可以创建一个unique_ptr来管理servicecontrolhandle.这个句柄类型不是通过调用delete来释放的,而是通过调用CloseServiceHandle()来释放的。.这是执行此操作的示例代

c++ - shared_ptr 与非指针资源

在C++11中是否可以使用shared_ptr来控制非指针资源?可以使用unique_ptr来管理非指针资源。这是通过实现一个自定义删除器类来完成的,该类提供:一个typedef{TYPE}指针;其中{TYPE}是非指针资源类型operator()(pointer)释放受控资源...然后使用自定义删除器作为第二个模板参数实例化一个unique_ptr。例如,在Windows下,可以创建一个unique_ptr来管理servicecontrolhandle.这个句柄类型不是通过调用delete来释放的,而是通过调用CloseServiceHandle()来释放的。.这是执行此操作的示例代

c++ - 临时对象的破坏顺序异常(exception)?

阅读C++0x草案的第1.9/14条。我找到了:Everyvaluecomputationandsideeffectassociatedwithafull-expressionissequencedbeforeeveryvaluecomputationandsideeffectassociatedwiththenextfull-expressiontobeevaluated.8)脚注8说8)Asspecifiedin12.2,afterafull-expressionisevaluated,asequenceofzeroormoreinvocationsofdestructorfunc

c++ - 临时对象的破坏顺序异常(exception)?

阅读C++0x草案的第1.9/14条。我找到了:Everyvaluecomputationandsideeffectassociatedwithafull-expressionissequencedbeforeeveryvaluecomputationandsideeffectassociatedwiththenextfull-expressiontobeevaluated.8)脚注8说8)Asspecifiedin12.2,afterafull-expressionisevaluated,asequenceofzeroormoreinvocationsofdestructorfunc

c++ - 使用 std::vector< std::shared_ptr<const T>> 是反模式吗?

很长一段时间我都在使用std::vector和std::shared_ptr手牵手。最近开始使用std::shared_ptr每当需要指向const对象的指针时。没关系,因为std::shared_ptr可以转换为std::shared_ptr然后他们共享相同的引用计数器,一切都感觉很自然。但是当我尝试使用std::vector>等结构时我遇到了麻烦。为简化起见,我将表示这两种结构:templateusingSharedPtrVector=std::vector>;templateusingSharedConstPtrVector=std::vector>;问题是虽然SharedPtr