Can you combine std::recursive_mutex with std::condition_variable?

You can, if you use std::condition_variable_any, which allows for any type of object that supports the Lockable concept. However, in the case of recursive mutex, you do have to ensure that the given thread has only locked the recursive mutex once, since the condition variable only will use the unlock method on the unique_lock once … Read more

How to hash std::string?

For a quick solution involving no external libraries, you can use hash<std::string> to hash strings. It’s defined by including the header files hash_map or unordered_map (or some others too). #include <string> #include <unordered_map> hash<string> hasher; string s = “heyho”; size_t hash = hasher(s); If you decide you want the added security of SHA, you don’t … Read more

Complexity of std::list::splice and other list containers

This was a very contentious topic during the standardization of C++11. The problem is that all standard containers, including lists, also have a constant-time size operation. Before C++11, many implementations made size linear time and splice between different lists constant time. C++11 now requires that size be constant and splice be linear. The problem is … Read more

Container of fixed dynamic size

Theoretically vector has the properties you need. As you noted, actions that possibly do assignments to the contained type, including especially any sequence modifications (empace_back, push_back, insert etc.) are not supported if the elements are noncopyable and/or nonassignable. So to create a vector of noncopyable elements, you’d have to construct each element during vector construction. … Read more

std::thread error (thread not member of std)

gcc does not fully support std::thread yet: http://gcc.gnu.org/projects/cxx0x.html http://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html Use boost::thread in the meantime. Edit Although the following compiled and ran fine for me with gcc 4.4.3: #include <thread> #include <iostream> struct F { void operator() () const { std::cout<<“Printing from another thread”<<std::endl; } }; int main() { F f; std::thread t(f); t.join(); return 0; … Read more

How to move from std::optional

It is valid to move from optional<T>::value() since it returns a mutable reference and the move does not destroy the object. If the optional instance is not engaged, value() will throw a bad_optional_access exception (§20.6.4.5). You explicitly check whether the option is engaged: if (content) Process(move(*content)); But you don’t use the member value() to access … Read more

tech