When I made my vector3 struct I noticed the operator functions were almost identical to eachother
vector3 vector3::operator+(float scalar) const {
return vector3(x + scalar, y + scalar, z + scalar);
}
// And…
vector3 vector3::operator*(float scalar) const {
return vector3(x * scalar, y * scalar, z * scalar);
}
I was able to copy the code from above and replace the + symbol to a *.
Is there any way of writing both of those functions in one so the overloads aren’t duplicated each time for each operator.
Something like using template
where I could define each operator in a sequence, and replace the + or the * in the previous code with a name.
template <operatorname o>
vector3 vector3::operator o(float scalar) const {
return vector3(x o scalar, y o scalar, z o scalar);
}