Inserting into an unordered_set with custom hash function

First problem: You are passing string as the second template argument for your instantiation of the unordered_set<> class template. The second argument should be the type of your hasher functor, and std::string is not a callable object. Perhaps meant to write: unordered_set<Interval, /* string */ Hash> test; // ^^^^^^^^^^^^ // Why this? Also, I would … Read more

Best way to represent Nullable member in C++? [duplicate]

You could look into Boost.Optional: struct Person { std::string Name; DateTime Birth; boost::optional<DateTime> Death; //… }; Your Death is “uninitialised” at first. You can then assign a value to it with =, like Death = myDateTime. When Death.is_initialized(), you can use Death.get(). Uninitialise it again with Death.reset(). For simple cases like this, though, it’s usually … Read more

How to check if weak_ptr is empty (non-assigned)?

You can use two calls to owner_before to check equality with a default constructed (empty) weak pointer: template <typename T> bool is_uninitialized(std::weak_ptr<T> const& weak) { using wt = std::weak_ptr<T>; return !weak.owner_before(wt{}) && !wt{}.owner_before(weak); } This will only return true if w{} “==” weak, where “==” compares owner, and according to en.cppreference.com: The order is such … Read more

Is synchronizing with `std::mutex` slower than with `std::atomic(memory_order_seq_cst)`?

Lockfree programming is about progress guarantees: From strongest to weakest, those are wait-free, lock-free, obstruction-free, and blocking. A guarantee is expensive and comes at a price. The more guarantees you want, the more you pay. Generally, a blocking algorithm or datastructure (with a mutex, say) has the greatest liberties, and thus is potentially the fastest. … Read more

std::unique_ptr with derived class

If they are polymorphic types and you only need a pointer to the derived type use dynamic_cast: Derived *derivedPointer = dynamic_cast<Derived*>(basePointer.get()); If they are not polymorphic types only need a pointer to the derived type use static_cast and hope for the best: Derived *derivedPointer = static_cast<Derived*>(basePointer.get()); If you need to convert a unique_ptr containing a … Read more

Is it a bad practice to always capture all in a lambda expression? [duplicate]

Performance The standard guarantees that if you do a default capture, the only variables that will be captured by that default capture from the surrounding environment are those that you actually use inside the lambda. As such, specifying individual variables to capture acts as documentation of what you expect to use, but should never affect … Read more

Copy std::vector into std::array

Use std::copy_n std::array<T, N> arr; std::copy_n(vec.begin(), N, arr.begin()); Edit: I didn’t notice that you’d asked about moving the elements as well. To move, wrap the source iterator in std::move_iterator. std::copy_n(std::make_move_iterator(v.begin()), N, arr.begin());

C++11 – typeid uniqueness

You should use std::type_index for mapping purposes. The type_index class is a wrapper class around a std::type_info object, that can be used as index in associative and unordered associative containers. The relationship with type_info object is maintained through a pointer, therefore type_index is CopyConstructible and CopyAssignable.

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