Reputation: 21
I have a templated class called Matrix with the following template parameters:
template <typename T, int rows, int columns>
class Matrix
{
...
}
I want to write a function called Augment that augments (just combines the two side by side) two matrices of different sizes into a new matrix. For example: 3x1 + 3x2 = 3x3.
Written in C++, the objects would look like this:
Matrix<int, 3, 1> a;
Matrix<int, 3, 2> b;
Matrix<int, 3, 3> c = a.Augment(b);
I have my class function declaration as follows:
template <int newColumns, int otherColumns>
Matrix<T, rows, newColumns> Augment(const Matrix<T, rows, otherColumns>& other)
However, I am getting an error:
no instance of function template
"Matrix<T, rows, columns::Augment [with T=int, rows=3, columns=1]" matches the argument list
argument types are: (Matrix<int, 3, 2>)
object type is: Matrix<int, 3, 1>.
I have tried these template parameters with no luck so far, I still get the same error:
template <int newColumns, int otherColumns>
template <typename T, int rows, int newColumns, int columns, int otherColumns>
Upvotes: 1
Views: 59
Reputation: 5830
With C++20, you could do something like this:
constexpr auto Augment(const auto& other)
{
constexpr auto new_cols = 3; //or whatever new columns is
return Matrix<T, rows, new_cols>{};
}
A full example can be seen here: https://godbolt.org/z/s5xnKWvcc
Upvotes: 0
Reputation: 29450
The compiler can't deduce newColumns
with the signature you've provided it. How about this:
template <int otherColumns>
Matrix<T, rows, columns+otherColumns> Augment(const Matrix<T, rows, otherColumns>& other);
Upvotes: 2