As i stated in the title, I just can't understand why does this function throw std::bad_alloc. If we take a look at the cppreference all of the three possible implementations are just as someone would assume and it looks like there is no special need dynamic memory allocation.