I have a basic (mathematical) vector class, which in my opinion benefits from C++'s operator overloading. Vector-scalar operations are defined as self-modifying functions in the class itself,
class Vec3i {
Vec3i& operator+=(int const n) {
for (int i = 0; i < 3; ++i) {
_data[i] += n;
}
}
std::array<int, 3> _data;
};
and as non-modifying free functions. For these I can see two options when it comes to passing in the vector.
// By value, meaning an implicit copy.
Vec3i operator+(Vec3i lhs, int const rhs) {
return (lhs += rhs);
}
// By const reference, copying manually.
Vec3i operator+(Vec3i const& lhs, int const rhs) {
auto result = lhs;
return (result += rhs);
}
Are there good reasons to prefer one variant over the other? I prefer having the const parameter (const-ing everything that will never be modified), but the by-value variant is nicely concise. Or should I simply start to read by-value as implicitly const?