C99 style variable length array function symbol in C ++

In C99, we can write function signatures like this:

void func(int dim1, int dim2, float A[dim1 * dim2]);

dim1and dim2are runtime parameters. This is good, since any user of such a function immediately has an idea of ​​the dimension Aand can thus output additional information without reading comments / documentation. This is especially true if dim1they dim2are parameters with semantics beyond what is shown here.

Is it possible to write an interface in C ++ that gives a hint about the dimension and size of the vector / tensor that the function expects? Dimension can probably be encoded as an argument to the template (I don’t like something in particular, but this is another topic), but size? Any ideas?

Update:

I have to make it clearer, I think. The C ++ function will look like this:

void func(int dim1, int dim2, std::vector<float> A);

and one of the two dimensions can even be omitted (as it is A.size()/dim). But the signature does not tell the user anything about the size of the vector that the function expects. Of course I can do assert(A.size() == dim1*dim2);the like. This is not what I am asking for. I ask how to make the interface more informative.

Update 2:

So I'm sure I want:

typedef int dim1; 
typedef int dim2; 
void func(Matrix<dim1, dim2> A);

As mentioned in the answer, the information should be in type - of course.

+4
source share
2 answers

In C ++ you would do void func(Matrix const& A). The sizes will be encoded in the matrix itself, so you will not need to transfer them separately. This ensures dimensional accuracy and simplifies the defiant idiom.

+5

std::array:

void func(std::array<int, /* dimension */> &arr);
0

Source: https://habr.com/ru/post/1545998/


All Articles