Is std::less supposed to allow comparison of unrelated pointers at compile-time?

I don’t think there’s a clear answer to the question that you’re asking. This is a specific case of LWG 2833: marking a library function constexpr does not explain the circumstances under which calling the function will yield a constant expression. Until this issue is resolved, I think you simply cannot rely on std::less being … Read more

How do I erase elements from STL containers?

Unfortunately, there isn’t a single uniform interface or pattern for erasing elements from STL containers. But three behaviors emerge: std::vector Pattern To erase elements that fulfill a certain condition from a std::vector, a common technique is the so called erase-remove idiom. If v is an instance of std::vector, and we want to erase elements with … Read more

Why does numeric_limits::min return a negative value for int but positive values for float/double?

By definition, for floating types, min returns the smallest positive value the type can encode, not the lowest. If you want the lowest value, use numeric_limits::lowest instead. Documentation: http://en.cppreference.com/w/cpp/types/numeric_limits/min As for why it is this way, I can only speculate that the Standard committee needed to have a way to represent all forms of extreme … Read more

Is the C++ std::set thread-safe?

STL has no built in thread support, so you’ll have to extend the STL code with your own synchronization mechanisms to use STL in a multithreaded environment. For example look here: link text Since set is a container class MSDN has following to say about the thread safety of the containers. A single object is … Read more

Why shared_timed_mutex is defined in c++14, but shared_mutex in c++17?

Shared mutex originally had timing in it, and was called shared_mutex. An implementor (msvc iirc) noted they could implement it “cheaper” without timing. In particular, SRWLOCK is an existing primitive on windows that is sufficient to implement shared mutex, but timed requires extra machinery. (Via @t.c.). (However, I believe it isn’t just easier because already … Read more

How std::bind works with member functions

When you say “the first argument is a reference” you surely meant to say “the first argument is a pointer“: the & operator takes the address of an object, yielding a pointer. Before answering this question, let’s briefly step back and look at your first use of std::bind() when you use std::bind(my_divide, 2, 2) you … Read more

Flattening iterator

I don’t know of any implementation in a major library, but it looked like an interesting problem so I wrote a basic implementation. I’ve only tested it with the test case I present here, so I don’t recommend using it without further testing. The problem is a bit trickier than it looks because some of … Read more