smart-pointers

Is there / should there be a way to track the validity of a raw pointer taken from a smart pointer for debug/QA purposes?

自作多情 提交于 2019-12-11 17:34:25
问题 As I understand C++ Core Guidelines by Bjarne Stroustrup & Herb Sutter, it is a good idea to use pointers this way: unique_ptr for the good old clear one ownership shared_ptr for truly shared ownership by design weak_ptr when by design the pointer might not be valid anymore raw pointer whenever the pointer is valid by design, and ownership is unchanged Thus theoretically it still may be a common bug that a raw pointer becomes invalid (see R37). I wonder if there is a way for debug/QA purposes

Clone and Cast Rc pointer

為{幸葍}努か 提交于 2019-12-11 14:55:08
问题 this is kind of a follow up question from this: Rust dynamic cast trait object between different taits The solution provided there works really well when we use references for trait objects. This time I was trying to do the same with Rc pointers. For example I have a super trait named TraitAB and 2 traits named TraitA and TraitB So when I first create a trait object of type TraitAB instead of using a Box , now I use an Rc pointer. I need a variable of type TraitA to be a reference of ab Here

Create object only if some condition, otherwise return nullptr

半城伤御伤魂 提交于 2019-12-11 08:16:28
问题 I want to create an object only if some conditions are applied, otherwise retun nullptr. This is how I would do it in Delphi (2009+): function GetGen(n : integer) : Generics.Collections.TList<Integer>; var i : integer; begin result := nil; if n > 0 then begin result := Generics.Collections.TList<Integer>.Create; for i := 0 to n - 1 do result.Add(i); end; end; procedure TestGenList(n : integer); var aInt : integer; aGen : Generics.Collections.TList<Integer>; begin aGen := GetGen(n); if aGen =

unique_ptr, qvector.resize() throws error 2280 attempting to reference a deleted function

删除回忆录丶 提交于 2019-12-11 07:35:43
问题 To prevent scope creep (on a previous Q), I've isolated the above error. My Voxel class definition: #ifndef VOXEL_H #define VOXEL_H #include <QObject> #include <QVector> #include <iostream> include <memory> class Voxel : public QObject { Q_OBJECT public: Voxel(); ~Voxel(); }; #endif // VOXEL_H The main file which triggers the error: #include <voxel.h> int main(int argc, char *argv[]) { QVector < QVector <std::unique_ptr <Voxel> > > cVoxel; cVoxel.resize(0); int rows = 80, cols = 80; for (int

Why does reassigning a smart pointer to itself cause destruction?

孤者浪人 提交于 2019-12-11 04:48:50
问题 TLDR Why does the line t_ptr = std::unique_ptr<Test>(t_ptr.get()); cause the destructor to be called? The line seems to just innocently assign t_ptr back to itself... Further, why am I able to continue calling methods after the supposed destruction? Example Code class Test { public: Test() { printf("Constructor called: %p\n", this); i = 0; }; void print() { printf("%d\n", i++); }; ~Test() { printf("Destructor called: %p\n", this); }; private: int i; }; int main(int argc, char** argv) { std:

C++ safely deleting event object payload with shared_ptr

≯℡__Kan透↙ 提交于 2019-12-11 04:26:34
问题 I need to create an Event object to be dispatched by an event listener system. The Event needs to have following properties: Event can potentially be handled by 0..n listener objects. Event contains a void pointer which can point to an arbitrary object (payload) (unknown type at build time). Event listener will convert to appropriate type depending on Event 's name. Need the payload object to be (automatically) deleted once the event has been dispatched to interested parties. Original event

Can you put a pimpl-Class inside a vector

被刻印的时光 ゝ 提交于 2019-12-11 03:59:53
问题 I have a class implemented using the PImpl Ideom: class FooImpl {}; class Foo { unique_ptr<FooImpl> myImpl; public: Foo(); ~Foo(); }; And now I want to put this into a std::vector void Bar() { vector<Foo> testVec; testVec.resize(10); } But when I do that, I get a compiler error (VC++ 2013) error C2280: 'std::unique_ptr>::unique_ptr(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)' : attempting to reference a deleted function I get the same error with testVec.emplace_back(); and testVec

Smart pointers and polymorphism

放肆的年华 提交于 2019-12-11 03:04:35
问题 I implemented reference counting pointers (called SP in the example) and I'm having problems with polymorphism which I think I shouldn't have. In the following code: SP<BaseClass> foo() { // Some logic... SP<DerivedClass> retPtr = new DerivedClass(); return retPtr; } DerivedClass inherits from BaseClass . With normal pointers this should have worked, but with the smart pointers it says "cannot convert from 'SP<T>' to 'const SP<T>&" and I think it refers to the copy constructor of the smart

Wrapping of C-code with a unique_ptr and custom deleter

£可爱£侵袭症+ 提交于 2019-12-11 02:47:40
问题 I'm trying to wrap an object from the C-API of OpenCV (CvPOSITObject) in a smart-pointer. To my understanding it should be something like the following: unique_ptr<CvPOSITObject, decltype(cvReleasePOSITObject)> positObject; positObject = unique_ptr<CvPOSITObject, decltype(cvReleasePOSITObject)>(cvCreatePOSITObject(param1, param2), cvReleasePOSITObject); But I get a compiler error, and google didn't really help. The declarations of the two functions are: CVAPI(CvPOSITObject*)

Eclipse polymorphism using C++11 shared_ptr error

别来无恙 提交于 2019-12-11 02:36:58
问题 Given the following sample code: #include <iostream> #include <memory> using namespace std; struct A { public: A(int aa) : a(aa) {} int a; virtual ~A() {} }; struct B : A { public: B(int aa, int bb) : A(aa), b(bb) {} int b; }; void f(shared_ptr<A> a){ shared_ptr<B> b = dynamic_pointer_cast<B>(a); if (b) { cout << b->b << endl; } else { cout << a->a << endl; } } int main() { auto a = make_shared<A>(3); auto b = make_shared<B>(7, 4); f(a); f(b); return 0; } Eclipse indicates that there is an