-3

I have several constant matrices of defferent sizes of both dimensions, say

const int denoise[][3] = {...}.

const int deconv[][4] = {...}

Then I define a function like void handleMatrix(const int* const*){...} hoping to handle these matrices. But it is uncorrect.

one effort I tried is using a template like:

template<typename Ty> void handle Matrix(const Ty m){...}

It works perfectly on vs2013.

But how should I pass those matrices to a function without using template?

Shindou
  • 506
  • 6
  • 15

2 Answers2

2

You should use a typedef so that you don't have to use any awful syntax:

using matrix_t = int[3][3];

And you should pass your args by reference whenever possible:

void handle_matrix(const matrix_t &mat){
    // do something with 'mat'
}

If you want to use the original syntax without a typedef:

void handle_matrix(const int (&mat)[3][3]){
    // ...
}

and if you want to use the original syntax and pass by pointer:

void handle_matrix(const int (*mat)[3]){
    // ...
}

But then you lose type safety, so I'd recommend against this and just go with the nicest option: typedef and pass by reference.


EDIT

You said in a comment on @Kerrek SB's answer that your matrices will be different sizes.

So here is how to handle that and still keep the nice method:

template<size_t Columns, size_t Rows>
using matrix_t = int[Columns][Rows];

template<size_t Columns, size_t Rows>
void handle_matrix(const matrix_t<Columns, Rows> &mat){
    // ...
}

Take into account that I'm presuming you can use C++14 in my answer, if you leave a comment I can modify it for any other version.

RamblingMad
  • 5,332
  • 2
  • 24
  • 48
  • @CoffeeandCode But my 2-dimension matrices have different sizes of both dimensions and I am defining this method in a template class. I wander if there is any method without using a template? – Shindou Jul 12 '15 at 13:04
  • If you want type safety, no. If you don't mind putting everything on the heap, yes. – RamblingMad Jul 12 '15 at 13:05
  • @CoffeeandCode But using a template is no difference with defining a function for each size of matrix.... – Shindou Jul 12 '15 at 13:10
  • @Shindou It's no different to the *compiler*, but it's much easier for you and saves code duplication. Are you trying to cut down the size of your executable? Most of the time it will have an extremely negligible effect. – RamblingMad Jul 12 '15 at 13:12
  • As for an array, I can define a function`foo(const int*)` to handle any size of array. Why is there no similar way for a 2-dimension array? – Shindou Jul 12 '15 at 13:24
  • Because the compiler needs to know what type the pointer is pointing to at compile time. `int` is what `int*` is pointing to. `int*` is what `int**` is pointing to, not an array. You could cast to a pointer if you *really* wanted to. – RamblingMad Jul 12 '15 at 13:27
  • What do you think if I define a matrix as an array and add some methods to compute the row and col indexes. Then all problems can be solved. – Shindou Jul 12 '15 at 13:34
  • @Shindou it's up to you, man. That may give you much better performance if you ever need to copy the matrix too. – RamblingMad Jul 12 '15 at 13:39
1

Your matrices are arrays of int[3]s. If you want C-style argument passing, you'd pass a pointer the first element of the array plus a size:

using Row = int[3];

void foo(const Row * p, std::size_t n_cols)
{
    for (std::size_t i = 0; i != n_cols; ++i)
    {
        for (int n : p[i]) { std::cout << n << ' '; }
        std::cout << '\n';
    }
}

Usage example:

Row * matrix = new Row[40]();
foo(matrix, 40);
delete [] matrix;

With a typed variable:

Row matrix[] = { {1,2,3}, {2,3,4} };
foo(matrix, std::distance(std::begin(matrix), std::end(matrix)));
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084