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

Based on janm’s response at first I did this: template<class T> std::shared_ptr<T> to_std(const boost::shared_ptr<T> &p) { return std::shared_ptr<T>(p.get(), [p](…) mutable { p.reset(); }); } template<class T> boost::shared_ptr<T> to_boost(const std::shared_ptr<T> &p) { return boost::shared_ptr<T>(p.get(), [p](…) mutable { p.reset(); }); } But then I realized I could do this instead: namespace { template<class SharedPointer> struct Holder { … Read more

map with incomplete value type

The standard requires that, all types used in template components of the standard library must be complete, unless otherwise stated. Thus libc++’s error is correct. Using an incomplete type is undefined behavior (§17.6.4.8[res.on.functions]/2), so libstdc++ accepting it isn’t wrong either. You could use a pointer object to construct the complete type as you did. But … Read more

What data structure is inside std::map in C++?

Step debug into g++ 6.4 stdlibc++ source Did you know that on Ubuntu’s 16.04 default g++-6 package or a GCC 6.4 build from source you can step into the C++ library without any further setup? By doing that we easily conclude that a Red-black tree used in this implementation. This makes sense, since std::map, unlike … Read more

I want to kill a std::thread using its thread object? [duplicate]

@bamboon’s answer is good, however I feel this deserves a stronger statement. Whatever the language you use, your program will acquire and release resources: memory, file descriptors, … For simple programs that are fired in one shots, leaking resources does not matter much: when the program ends modern OSes automatically take the resources back; however … Read more

what does `using std::swap` inside the body of a class method implementation mean?

This mechanism is normally used in templated code, i.e. template <typename Value> class Foo. Now the question is which swap to use. std::swap<Value> will work, but it might not be ideal. There’s a good chance that there’s a better overload of swap for type Value, but in which namespace would that be? It’s almost certainly … Read more

C++ std::tuple order of destruction

I’ll offer a life lesson I’ve learned, rather than a direct answer, in response to your question: If you can formulate, for multiple alternatives, a reasonable argument for why that alternative should be the one mandated by the standard – then you should not assume any of them is mandated (even if one of them … Read more