草庐IT

unique-constraint

全部标签

c++ - unique_ptr 是否保证在 move 后存储 nullptr?

unique_ptr是否保证在move后存储nullptr?std::unique_ptrp1{newint{23}};std::unique_ptrp2{std::move(p1)};assert(!p1);//isthisalwaystrue? 最佳答案 可以,你可以在move之后和nullptr比较,保证比较相等。来自§20.8.1/4[unique.ptr]Additionally,ucan,uponrequest,transferownershiptoanotheruniquepointeru2.Uponcompletio

c++ - 如何声明 std::unique_ptr 以及它的用途是什么?

我试图了解std::unique_ptr的工作原理,为此我找到了this文档。作者从下面的例子开始:#include//declarationsofunique_ptrusingstd::unique_ptr;//defaultconstructionunique_ptrup;//createsanemptyobject//initializewithanargumentunique_ptruptr(newint(3));double*pd=newdouble;unique_ptruptr2(pd);//overloaded*and->*uptr2=23.5;unique_ptrups

c++ - C++11 unique_ptr 和 shared_ptr 是否能够转换为彼此的类型?

C++11标准库是否提供任何实用程序来将std::shared_ptr转换为std::unique_ptr,反之亦然?这样操作安全吗? 最佳答案 std::unique_ptristheC++11waytoexpressexclusiveownership,butoneofitsmostattractivefeaturesisthatiteasilyandefficientlyconvertstoastd::shared_ptr.Thisisakeypartofwhystd::unique_ptrissowellsuitedasaf

c++ - 为什么在 C++17 中使用 std::make_unique?

据我了解,C++14引入了std::make_unique因为,由于未指定参数评估顺序,这是不安全的:f(std::unique_ptr(newMyClass(param)),g());//SyntaxA(解释:如果评估首先为原始指针分配内存,然后调用g(),并在std::unique_ptr构造之前抛出异常,则内存泄漏。)调用std::make_unique是一种限制调用顺序的方法,从而使事情变得安全:f(std::make_unique(param),g());//SyntaxB从那时起,C++17已经澄清了评估顺序,使得语法A也安全,所以这是我的问题:还有使用std::make_

c++ - 如何使用带有 std::unique_ptr 成员的自定义删除器?

我有一个包含unique_ptr成员的类。classFoo{private:std::unique_ptrbar;...};Bar是具有create()函数和destroy()函数的第三方类。如果我想在一个独立的函数中使用std::unique_ptr,我可以这样做:voidfoo(){std::unique_ptrbar(create(),[](Bar*b){destroy(b);});...}有没有办法将std::unique_ptr作为类的成员? 最佳答案 假设create和destroy是具有以下签名的自由函数(这似乎是OP

ios - 在自动布局中将 subview 的 X 居中抛出 "not prepared for the constraint"

我有一个自定义UIView子类,它正在通过nib初始化。在-awakeFromNib中,我正在创建一个subview并尝试将其置于其父View的中心。[selfsetInteralView:[[UIViewalloc]init]];[[selfinternalView]addConstraint:[NSLayoutConstraintconstraintWithItem:[selfinternalView]attribute:NSLayoutAttributeCenterXrelatedBy:NSLayoutRelationEqualtoItem:selfattribute:NSLay

php - Laravel 迁移 : unique key is too long, 即使指定

我正在尝试在Laravel中迁移用户表。当我运行迁移时,出现此错误:[Illuminate\Database\QueryException]SQLSTATE[42000]:Syntaxerrororaccessviolation:1071Specifiedkeywastoolong;maxkeylengthis767bytes(SQL:altertableusersadduniqueusers_email_uniq(email))我的迁移如下:Schema::create('users',function(Blueprint$table){$table->increments('id'

C++ unique_ptr 和映射

我正在尝试使用C++0xunique_ptrclass在map内像这样://compilewith`g++main.cpp-std=gnu++0x`#include#include#includeusingnamespacestd;structFoo{char*str;Foo(charconst*str_):str(strdup(str_)){}};intmain(void){typedefstd::map>Bar;Barbar;autoa=bar.insert(Bar::value_type(1,newFoo("one")));return0;}但是GCC给了我以下错误(缩短,我认为

C++ unique_ptr 和映射

我正在尝试使用C++0xunique_ptrclass在map内像这样://compilewith`g++main.cpp-std=gnu++0x`#include#include#includeusingnamespacestd;structFoo{char*str;Foo(charconst*str_):str(strdup(str_)){}};intmain(void){typedefstd::map>Bar;Barbar;autoa=bar.insert(Bar::value_type(1,newFoo("one")));return0;}但是GCC给了我以下错误(缩短,我认为

c++ - std::auto_ptr 到 std::unique_ptr

随着新标准的到来(以及某些编译器中已经提供的部分),新类型std::unique_ptr应该是std::auto_ptr的替代品.它们的用法是否完全重叠(所以我可以对我的代码进行全局查找/替换(不是我会这样做,但如果我这样做了))或者我是否应该知道一些从阅读中不明显的差异文档?另外如果是直接替换,为什么要给它一个新名字而不是仅仅改进std::auto_ptr? 最佳答案 您无法进行全局查找/替换,因为您可以复制auto_ptr(具有已知后果),但只能移动unique_ptr。任何看起来像的东西std::auto_ptrp(newin