When should I use std::bind?

Here’s something you can’t do with a lambda: std::unique_ptr<SomeType> ptr = …; return std::bind(&SomeType::Function, std::move(ptr), _1, _2); Lambdas can’t capture move-only types; they can only capture values by copy or by lvalue reference. Though admittedly this is a temporary issue that’s being actively resolved for C++14 ๐Ÿ˜‰ “Simpler and clearer” is a matter of opinion. … Read more

Should you prefer overloading over specialization of function templates?

Short story: overload when you can, specialise when you need to. Long story: C++ treats specialisation and overloads very differently. This is best explained with an example. template <typename T> void foo(T); template <typename T> void foo(T*); // overload of foo(T) template <> void foo<int>(int*); // specialisation of foo(T*) foo(new int); // calls foo<int>(int*); Now … Read more

Why do iterators need to be default-constructible

Forward iterators and stronger are required to refer to some external sequence (see [forward.iterators]/6 which says “If a and b are both dereferenceable, then a == b if and only if *a and *b are bound to the same object.”) This means they are generally just a lightweight handle onto something else (e.g. a pointer … Read more

What are the differences amongst Python’s “__get*__” and “_del*__” methods?

The documentation for every method that you listed is easly reachable from the documentation index . Anyway this may be a little extended reference: __get__, __set__ and __del__ are descriptors “In a nutshell, a descriptor is a way to customize what happens when you reference an attribute on a model.” [official doc link] They are … Read more

Is it legal to pass a non-null-terminated string to strncmp in C?

According to the C99 standard, section 7.21.4.4, ยง3., it is legal: The strncmp function returns an integer greater than, equal to, or less than zero, accordingly as the possibly null-terminated array pointed to by s1 is greater than, equal to, or less than the possibly null-terminated array pointed to by s2. Notice, however, that it … Read more

C++11 Thread waiting behaviour: std::this_thread::yield() vs. std::this_thread::sleep_for( std::chrono::milliseconds(1) )

The Standard is somewhat fuzzy here, as a concrete implementation will largely be influenced by the scheduling capabilities of the underlying operating system. That being said, you can safely assume a few things on any modern OS: yield will give up the current timeslice and re-insert the thread into the scheduling queue. The amount of … Read more

Does std::mt19937 require warmup?

Mersenne Twister is a shift-register based pRNG (pseudo-random number generator) and is therefore subject to bad seeds with long runs of 0s or 1s that lead to relatively predictable results until the internal state is mixed up enough. However the constructor which takes a single value uses a complicated function on that seed value which … Read more