c++11 - c++ function name ambiguity -
my class has 2 functions same name , following signature:
mat<t, rows, cols> & transpose() { (size_t = 0; < rows; ++i) { (size_t j = + 1; j < cols; ++j) { std::swap(at(i, j), at(j, i)); } } return *this; } this method inplace transpose of matrix. now, have function leaves original matrix unchanged , transpose in new matrix. signature is:
mat<t, cols, rows> transpose() const note columns , rows swapped.
now, call as:
mat<int, 3, 4> d; // fill matrix mat<int, 4, 3> e = d.transpose(); this still tries call first method. if rename second method transpose2 , call that, fine. there way make these 2 functions inambiguous?
the overload resolution doesn't depend on return value, it's determined function parameters match arguments closely. have make object being called on const, make const member function overload called. e.g.
mat<int, 4, 3> e = const_cast<const mat<int, 3, 4>&>(d).transpose();
Comments
Post a Comment