[[nodiscard]] int& top(const int idx) noexcept {
assert(!isTopEdge(idx));
return matrixData[topIndex(idx)];
}
[[nodiscard]] int top(const int idx) const noexcept {
assert(!isTopEdge(idx));
return matrixData[topIndex(idx)];
}
In this scenario, the only differentiating factor is the function signature, it is implicitly determined whether the return value is going to be by value or reference based on the function signature, and one of them is const.
Is there any way that I could reduce the copied code by possibly using templates? Can I create a generic method, and then create two prototypes of it, one using a reference and one without, and make the one without the reference const?