shared-ptr

Is a virtual destructor needed for your Interface, if you always store it in a shared_ptr?

瘦欲@ 提交于 2020-08-26 19:57:09
问题 Since boost::/std::shared_ptr have the advantage of type-erasing their deleter, you can do nice things like #include <memory> typedef std::shared_ptr<void> gc_ptr; int main(){ gc_ptr p1 = new int(42); gc_ptr p2 = new float(3.14159); gc_ptr p3 = new char('o'); } And this will correctly delete all pointer thanks to the correct deleter being saved. If you ensure that every implementation of your interface always gets created with shared_ptr<Interface> (or make_shared<Interface> ), do you

Is a virtual destructor needed for your Interface, if you always store it in a shared_ptr?

时间秒杀一切 提交于 2020-08-26 19:54:32
问题 Since boost::/std::shared_ptr have the advantage of type-erasing their deleter, you can do nice things like #include <memory> typedef std::shared_ptr<void> gc_ptr; int main(){ gc_ptr p1 = new int(42); gc_ptr p2 = new float(3.14159); gc_ptr p3 = new char('o'); } And this will correctly delete all pointer thanks to the correct deleter being saved. If you ensure that every implementation of your interface always gets created with shared_ptr<Interface> (or make_shared<Interface> ), do you

How to detect if a type is shared_ptr at compile time

杀马特。学长 韩版系。学妹 提交于 2020-08-02 04:04:45
问题 I want to get a templatized way of finding if a type is a shared_ptr and based on that I want to have a new specialization of a function. Example main function is, template <class T> inline void CEREAL_LOAD_FUNCTION_NAME( RelaxedJSONInputArchive & ar, NameValuePair<T> & t ) { std::cout << " CEREAL_LOAD_FUNCTION_NAME NameValuePair 1 " << std::endl; ar.setNextName( t.name ); ar( t.value ); } If t.value is shared_ptr then I want to have a different function specialization. I have tried below,

Storing an std::thread in C++11 smart pointer

拥有回忆 提交于 2020-07-05 07:57:13
问题 In C++ 11 & above what are the advantages or disadvantages when storing an std::thread as a member of class directly like so: std::thread my_thread; As opposed to storing a std::shared_ptr or std::unique_ptr to the thread like so: std::shared_ptr<std::thread> my_thread_ptr; Is any of the code options better than other? Or it doesn't matter, just 2 separate ways of handling the thread object. 回答1: May be there is some less common reason for usage of pointer (or smart pointer) member but for

error: no > match for call to ‘(boost::_mfi::mf1<void

最后都变了- 提交于 2020-06-29 03:56:06
问题 I have rather a basic knowledge in c++ and there is an issue described in the following which is likely rather simple and a based on a syntax mistake but I haven't figured out a way around it. Basically the error I get says: remote_hw_interface_node.cpp:23:68: required from here /usr/include/boost/function/function_template.hpp:231:11: error: no match for call to ‘(boost::_mfi::mf1<void, ROBOTHardwareInterface, const boost::shared_ptr<sensor_msgs::JointState_<std::allocator<void> > > >&>)

About “circular reference”, I used weak_ptr but memory leak still happened

一笑奈何 提交于 2020-06-26 07:42:32
问题 I read: How to avoid memory leak with shared_ptr? I know that I need to use weak_ptr to avoid circular reference . So I created a little program to play circular reference . Following object(spyder) will be invoke class spyder { public: spyder(std::string _name): m_name(_name), finger(nullptr) { } inline const std::string ask_name() const{ return m_name; } std::shared_ptr<spyder> finger; private: std::string m_name; }; I invoke spyder in my main code with shared_ptr and weak_ptr: int main(){

Using shared_from_this in templated classes

[亡魂溺海] 提交于 2020-05-25 03:41:10
问题 I have a resource manager that, like Andrei Alexandrescu proposed in the book Modern C++ Design, follows a policy based design. I am having trouble though, because my resource manager needs to be able to provide references to itself to the managed resources by shared_from_this() . I built a minimal example reproducing my problem, which results you can see here. Basically I have some managed resource that needs a reference to its manager: template <typename T> class managed_resource { typedef

having difficulties to make a vector of polymorphic objects using shared_ptr and unique_ptr in C++ Visual studio 2019 Cmake project

吃可爱长大的小学妹 提交于 2020-05-16 03:54:08
问题 For an exercise i need to build a vector of polymorphic objects and for some reason both shared and Unique ptr make linkage errors 2019 and 1120 when i use them. i have no option to use the old way of memory allocation with New and Delete so i have to make it work. i tried various of different syntax options for doing this and still with no luck. *note: we use Cmake to bind the project together in visual studio and also we splitting the objects into header and cpp files. here are my objects

C++11: extending std::is_pointer to std::shared_ptr

吃可爱长大的小学妹 提交于 2020-05-11 04:05:16
问题 I think about overloading std::is_pointer in C++11 to yield true for std::shared_ptr<T> as well, since the latter behaves very much as a T* . #include <type_traits> namespace std { template <typename T> struct is_pointer<shared_ptr<T>> : std::true_type {}; template <typename T> struct is_pointer<shared_ptr<T const>> : std::true_type {}; } I wonder why this overload has not already been included in the standard implementation. Is there a pitfall that I'm overlooking? As an alternative one