Differences between unique_ptr and shared_ptr [duplicate]

Both of these classes are smart pointers, which means that they automatically (in most cases) will deallocate the object that they point at when that object can no longer be referenced. The difference between the two is how many different pointers of each type can refer to a resource. When using unique_ptr, there can be … Read more

When is std::weak_ptr useful?

std::weak_ptr is a very good way to solve the dangling pointer problem. By just using raw pointers it is impossible to know if the referenced data has been deallocated or not. Instead, by letting a std::shared_ptr manage the data, and supplying std::weak_ptr to users of the data, the users can check validity of the data … Read more

Rule-of-Three becomes Rule-of-Five with C++11? [closed]

I’d say the Rule of Three becomes the Rule of Three, Four and Five: Each class should explicitly define exactly one of the following set of special member functions: None Destructor, copy constructor, copy assignment operator In addition, each class that explicitly defines a destructor may explicitly define a move constructor and/or a move assignment … Read more

Difference in make_shared and normal shared_ptr in C++

The difference is that std::make_shared performs one heap-allocation, whereas calling the std::shared_ptr constructor performs two. Where do the heap-allocations happen? std::shared_ptr manages two entities: the control block (stores meta data such as ref-counts, type-erased deleter, etc) the object being managed std::make_shared performs a single heap-allocation accounting for the space necessary for both the control block … Read more

Start thread with member function

#include <thread> #include <iostream> class bar { public: void foo() { std::cout << “hello from member function” << std::endl; } }; int main() { std::thread t(&bar::foo, bar()); t.join(); } EDIT: Accounting your edit, you have to do it like this: std::thread spawn() { return std::thread(&blub::test, this); } UPDATE: I want to explain some more points, … Read more

C++11 reverse range-based for-loop

Actually Boost does have such adaptor: boost::adaptors::reverse. #include <list> #include <iostream> #include <boost/range/adaptor/reversed.hpp> int main() { std::list<int> x { 2, 3, 5, 7, 11, 13, 17, 19 }; for (auto i : boost::adaptors::reverse(x)) std::cout << i << ‘\n’; for (auto i : x) std::cout << i << ‘\n’; }

What are inline namespaces for?

Inline namespaces are a library versioning feature akin to symbol versioning, but implemented purely at the C++11 level (ie. cross-platform) instead of being a feature of a specific binary executable format (ie. platform-specific). It is a mechanism by which a library author can make a nested namespace look and act as if all its declarations … Read more

How do I activate C++ 11 in CMake?

CMake 3.1 introduced the CMAKE_CXX_STANDARD variable that you can use. If you know that you will always have CMake 3.1 or later available, you can just write this in your top-level CMakeLists.txt file, or put it right before any new target is defined: set (CMAKE_CXX_STANDARD 11) If you need to support older versions of CMake, … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)