Std::copy and trivial copyability

If an object is trivially copyable, then it’s a good idea to use std::copy.

From https://en.cppreference.com/w/cpp/algorithm/copy

In practice, implementations of std::copy avoid multiple assignments and use bulk copy functions such as std::memmove if the value type is TriviallyCopyable

It turns out our var are trivially copyable as the following passes:

EXPECT_TRUE(std::is_trivially_copyable<stan::math::var>::value);

The new traits metaprograms are really nice in C++11.

2 Likes