In C ++ 11, there are many signs to know whether it is safe to perform bit operations or not.
In your case, I think you should use std::is_trivially_move_constructible<T> . These features are implemented using the built-in compiler functions (not portable, therefore, in the standard library), but they themselves are portable.
Therefore, the code should be akin to:
template <typename T> void move_to(T* storage, T* begin, T* end) { if (std::is_trivially_move_constructible<T>::value) { memmove(storage, begin, (end - begin) * sizeof(T)); } else { for (; begin != end; ++begin, ++storage) { new (storage) T(std::move(*begin)); } } }
And the compiler will optimize if at compile time depending on whether the type is a trivial movement constructive or not, and only leave an interesting branch.
source share