What is the difference between std::shared_ptr and std::atomic aka. std::experimental::atomic_shared_ptr?

The atomic “thing” in shared_ptr is not the shared pointer itself, but the control block it points to. meaning that as long as you don’t mutate the shared_ptr across multiple threads, you are ok. do note that copying a shared_ptr only mutates the control block, and not the shared_ptr itself. std::shared_ptr<int> ptr = std::make_shared<int>(4); for … Read more

What’s the difference between first locking and creating a lock_guard(adopt_lock) and creating a unique_lock(defer_lock) and locking?

1) First code sample { static std::mutex io_mutex; std::lock_guard<std::mutex> lk(io_mutex); std::cout << e1.id << ” and ” << e2.id << ” are waiting for locks” << std::endl; } This is a standard lock guard, when the scope is exited, the lock lk is released { std::unique_lock<std::mutex> lk1(e1.m, std::defer_lock); std::unique_lock<std::mutex> lk2(e2.m, std::defer_lock); std::lock(lk1, lk2); std::cout << … Read more

Is there any way to enforce that instances are only ever on the stack?

Disclaimer: ‘stack’ is not part of the c++ standard to my knowledge, there we have ASDVs (automatic storage duration variables). ABI might define stack. Note that sometimes these are passed in registers, which I believe is OK in your case. Define a CPS (continuation-passing style) factory method: class A { public: template<typename F, typename… Args> … Read more

Is it okay to “Move” an object from a queue, if you’re about to pop from it?

Yes, this is perfectly safe: std::queue<T> q; // add stuff… T top = std::move(q.front()); q.pop(); pop() doesn’t have any preconditions on the first element in the q having a specified state, and since you’re not subsequently using q.front() you don’t have to deal with that object being invalidated any more. Sounds like a good idea … Read more

Is there a shorthand for std::lock_guard lock(m)?

For pre-C++17: template<class Mutex> std::lock_guard<Mutex> make_lock_guard(Mutex& mutex) { mutex.lock(); return { mutex, std::adopt_lock }; } Use as: std::mutex someMutex; auto&& lg = make_lock_guard(someMutex); This takes advantage of the fact that copy-list-initialization doesn’t create an additional temporary (even conceptually). The one-parameter constructor is explicit and can’t be used for copy-list-initialization, so we lock the mutex first … Read more

Is it possible to use function return type as an argument in declaration of another function in C++?

decltype (MyFunction_1) will give you the type of MyFunction_1 (i.e. the function type std::tuple<int, bool, double> ()), you need to emulate a function calling 1 (via adding ()) to get the return type (i.e. std::tuple<int, bool, double>), e.g. void MyFunction_2 (decltype (MyFunction_1()) &params); // ^^ 1 The expression is evaluated at compile-time, the function won’t … Read more

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