سؤال

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?

هل كانت مفيدة؟

المحلول

In older C++ (pre C++11), there is no significant difference between your two implementations of operator+. Either the copy constructor gets called when you invoke the operator or when you make the explicit copy inside the operator.
In this case, it is more of a personal preference/coding guideline issue which one to choose.

With the introduction of move constructors in C++11, the pass-by-value case has gained an advantage, because that allows the parameter to be constructed using either the copy constructor or the move constructor, depending on what the operator is being invoked with.
For your Vec3i class, the difference is not that big, but if you have classes that maintain dynamically allocated resources, the proper use of move constructors can significantly reduce the amount of memory that your application needs.

نصائح أخرى

This is probably a bit opinionated, but if I see two equivalent alternatives in code, I typically prefer the one with less "noise". In your case, the difference of the second alternative to the first is only in additional "noise" - technical code which does not improve the readability in any way. That is why I would prefer the first variant.

or should I simply start to read by-value as implicitly const

You should read functions with "by value" arguments as side-effect free, if that is what you meant.

I would go for the take by value approach.

Maybe in this case it does not matter as the Vec3i is cheap to copy, but in general, this would allow the lhs value to be constructed from rvalue with a move constructor, thus removing any copying of objects.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى softwareengineering.stackexchange
scroll top