shared-ptr

Should I std::move a shared_ptr in a move constructor?

假如想象 提交于 2019-11-27 02:14:16
问题 Consider: #include <cstdlib> #include <memory> #include <string> #include <vector> #include <algorithm> #include <iterator> using namespace std; class Gizmo { public: Gizmo() : foo_(shared_ptr<string>(new string("bar"))) {}; Gizmo(Gizmo&& rhs); // Implemented Below private: shared_ptr<string> foo_; }; /* // doesn't use std::move Gizmo::Gizmo(Gizmo&& rhs) : foo_(rhs.foo_) { } */ // Does use std::move Gizmo::Gizmo(Gizmo&& rhs) : foo_(std::move(rhs.foo_)) { } int main() { typedef vector<Gizmo>

Boost async_* functions and shared_ptr's

两盒软妹~` 提交于 2019-11-27 01:43:59
I frequently see this pattern in code, binding shared_from_this as the first parameter to a member function and dispatching the result using an async_* function. Here's an example from another question: void Connection::Receive() { boost::asio::async_read(socket_,boost::asio::buffer(this->read_buffer_), boost::bind(&Connection::handle_Receive, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); } The only reason to use shared_from_this() instead of this is to keep the object alive until the member function gets called. But unless there's some

How can boost::serialization be used with std::shared_ptr from C++11?

只愿长相守 提交于 2019-11-27 01:31:50
问题 I know that there is a Boost module for serialization of boost::shared_ptr, but I cannot find anything for std::shared_ptr. Also, I don't know how to implement it easily. I'm afraid that the following code namespace boost{namespace serialization{ template<class Archive, class T> inline void serialize(Archive & ar, std::shared_ptr<T> &t, const unsigned int version) { if(Archive::is_loading::value) {T*r;ar>>r;t=r;} else {ar<<t.get();} } }}//namespaces doesn't work. Indeed, if some object was

Does C++11 unique_ptr and shared_ptr able to convert to each other's type?

喜夏-厌秋 提交于 2019-11-27 00:09:18
问题 Does C++11 standard library provide any utility to convert from a std::shared_ptr to std::unique_ptr , or vice versa? Is this safe operation? 回答1: std::unique_ptr is the C++11 way to express exclusive ownership, but one of its most attractive features is that it easily and efficiently converts to a std::shared_ptr . This is a key part of why std::unique_ptr is so well suited as a factory function return type. Factory functions can’t know whether callers will want to use exclusive ownership

Cohabitation of boost::shared_ptr and std::shared_ptr

穿精又带淫゛_ 提交于 2019-11-26 23:56:54
问题 I want to use boost::log at some point, but I cannot pass a std::shared_ptr as a parameter, because the compiler (VS2010) cannot convert it into a boost::shared_ptr . I don't really like the fact that they are aliens to one another. Is there a safe and transparent way to convert one into the another, so as they don't stumble over each other? I don't think it is duplicate of this question that states both are the same. 回答1: You could do it like this: template<typename T> boost::shared_ptr<T>

Passing shared pointers as arguments

匆匆过客 提交于 2019-11-26 21:23:47
If I declare an object wrapped in a shared pointer: std::shared_ptr<myClass> myClassObject(new myClass()); then I wanted to pass it as an argument to a method: DoSomething(myClassObject); //the called method void DoSomething(std::shared_ptr<myClass> arg1) { arg1->someField = 4; } Does the above simply increment the shared_pt's reference count and everything is cool? Or does it leave a dangling pointer? Are you still supposed to do this?: DoSomething(myClassObject.Get()); void DoSomething(std::shared_ptr<myClass>* arg1) { (*arg1)->someField = 4; } I think that the 2nd way may be more efficient

Conversion from boost::shared_ptr to std::shared_ptr?

青春壹個敷衍的年華 提交于 2019-11-26 19:17:41
问题 I got a library that internally uses Boost's version of shared_ptr and exposes only those. For my application, I'd like to use std::shared_ptr whenever possible though. Sadly, there is no direct conversion between the two types, as the ref counting stuff is implementation dependent. Is there any way to have both a boost::shared_ptr and a std::shared_ptr share the same ref-count-object? Or at least steal the ref-count from the Boost version and only let the stdlib version take care of it? 回答1:

std::shared_ptr thread safety

随声附和 提交于 2019-11-26 18:30:39
I've read that "Multiple threads can simultaneously read and write different shared_ptr objects, even when the objects are copies that share ownership." ( MSDN: Thread Safety in the Standard C++ Library ) Does that mean that changing shared_ptr object is safe ? For an instance, is the next code considered safe: shared_ptr<myClass> global = make_shared<myClass>(); ... //In thread 1 shared_ptr<myClass> private = global; ... //In thread 2 global = make_shared<myClass>(); ... Can I be sure in that case that thread 1 private will have the original value of global or the new value which thread 2

Example to use shared_ptr?

孤街醉人 提交于 2019-11-26 17:55:52
问题 Hi I asked a question today about How to insert different types of objects in the same vector array and my code in that question was gate* G[1000]; G[0] = new ANDgate() ; G[1] = new ORgate; //gate is a class inherited by ANDgate and ORgate classes class gate { ..... ...... virtual void Run() { //A virtual function } }; class ANDgate :public gate {..... ....... void Run() { //AND version of Run } }; class ORgate :public gate {..... ....... void Run() { //OR version of Run } }; //Running the

shared_from_this causing bad_weak_ptr

谁都会走 提交于 2019-11-26 16:20:23
问题 I am trying to keep a list of connected clients in asio. I have adapted the chat server example from the docs (http://www.boost.org/doc/libs/1_57_0/doc/html/boost_asio/example/cpp03/chat/chat_server.cpp) and here's the important part of what I ended up with: #include <iostream> #include <boost/bind.hpp> #include <boost/shared_ptr.hpp> #include <boost/enable_shared_from_this.hpp> #include <boost/asio.hpp> #include <set> using boost::asio::ip::tcp; class tcp_connection; std::set<boost::shared