草庐IT

auto_ptr_ref

全部标签

c++ - 为什么允许 shared_ptr<T[N]>?

Thisanswer引用N4082,这表明即将对std::shared_ptr进行的更改将允许T[]和T[N]变种:Unliketheunique_ptrpartialspecializationforarrays,bothshared_ptrandshared_ptrwillbevalidandbothwillresultindelete[]beingcalledonthemanagedarrayofobjects.templateexplicitshared_ptr(Y*p);Requires:Yshallbeacompletetype.Theexpressiondelete[]p

c++ - 在 dll 接口(interface)中使用 shared_ptr

我的dll中有一个抽象类。classIBase{protected:virtual~IBase()=0;public:virtualvoidf()=0;};我想在加载dll的exe文件中获取IBase。第一种方法是创建以下函数IBase*CreateInterface();并在IBase中添加虚函数Release()。第二种方法是创建另一个函数boost::shared_ptrCreateInterface();并且不需要Release()函数。问题。1)在第二种情况中,在dll(不是在exe文件中)调用析构函数和内存释放是真的吗?2)如果exe文件和dll使用不同的编译器(或不同的设

c++ - 在 dll 接口(interface)中使用 shared_ptr

我的dll中有一个抽象类。classIBase{protected:virtual~IBase()=0;public:virtualvoidf()=0;};我想在加载dll的exe文件中获取IBase。第一种方法是创建以下函数IBase*CreateInterface();并在IBase中添加虚函数Release()。第二种方法是创建另一个函数boost::shared_ptrCreateInterface();并且不需要Release()函数。问题。1)在第二种情况中,在dll(不是在exe文件中)调用析构函数和内存释放是真的吗?2)如果exe文件和dll使用不同的编译器(或不同的设

c++ - 在类内部,为什么 `auto b() -> decltype(a()) {}` 有效,但 `decltype(a()) b() {}` 无效?

考虑以下代码:(Ideone)structS{inta(){return0;}decltype(a())b(){return1;}};它给了我以下错误:error:cannotcallmemberfunction'intS::a()'withoutobject另一方面,这段代码编译得很好:(Ideone)structS{inta(){return0;}autob()->decltype(a()){return1;}};为什么一个例子有效,而另一个却编译失败?两个示例中的编译器行为是否完全正确?如果编译器是正确的,那么为什么标准会要求这种奇怪的行为? 最佳答案

c++ - 在类内部,为什么 `auto b() -> decltype(a()) {}` 有效,但 `decltype(a()) b() {}` 无效?

考虑以下代码:(Ideone)structS{inta(){return0;}decltype(a())b(){return1;}};它给了我以下错误:error:cannotcallmemberfunction'intS::a()'withoutobject另一方面,这段代码编译得很好:(Ideone)structS{inta(){return0;}autob()->decltype(a()){return1;}};为什么一个例子有效,而另一个却编译失败?两个示例中的编译器行为是否完全正确?如果编译器是正确的,那么为什么标准会要求这种奇怪的行为? 最佳答案

C++11 将 `auto` Lambda 更改为不同的 Lambda?

假设我有以下包含lambda的变量:autoa=[]{returntrue;};我希望a稍后返回false。我可以按照这个思路做点什么吗?a=[]{returnfalse;};这种语法给了我以下错误:binary'=':nooperatorfoundwhichtakesaright-handoperandoftype'main::'(orthereisnoacceptableconversion)IntelliSense:nooperator"="matchestheseoperandsoperandtypesare:lambda[]bool()->bool=lambda[]bool(

C++11 将 `auto` Lambda 更改为不同的 Lambda?

假设我有以下包含lambda的变量:autoa=[]{returntrue;};我希望a稍后返回false。我可以按照这个思路做点什么吗?a=[]{returnfalse;};这种语法给了我以下错误:binary'=':nooperatorfoundwhichtakesaright-handoperandoftype'main::'(orthereisnoacceptableconversion)IntelliSense:nooperator"="matchestheseoperandsoperandtypesare:lambda[]bool()->bool=lambda[]bool(

c++ - unique_ptr 与 shared_ptr 中的删除器类型

这个问题在这里已经有了答案:Whydoesunique_ptrtaketwotemplateparameterswhenshared_ptronlytakesone?(2个回答)关闭7年前。当我发现标准以两种完全不同的方式定义了std::unique_ptr和std::shared_ptr时,我觉得这非常奇怪可能拥有。这是来自cppreference::unique_ptr的声明和cppreference::shared_ptr:template>classunique_ptr;templateclassshared_ptr;如您所见,unique_ptr将Deleter对象的类型“保

c++ - unique_ptr 与 shared_ptr 中的删除器类型

这个问题在这里已经有了答案:Whydoesunique_ptrtaketwotemplateparameterswhenshared_ptronlytakesone?(2个回答)关闭7年前。当我发现标准以两种完全不同的方式定义了std::unique_ptr和std::shared_ptr时,我觉得这非常奇怪可能拥有。这是来自cppreference::unique_ptr的声明和cppreference::shared_ptr:template>classunique_ptr;templateclassshared_ptr;如您所见,unique_ptr将Deleter对象的类型“保

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