C++ Logo


Advanced search

Re: [wg14/wg21 liaison] Multidimensional subscript operator

From: Uecker, Martin <Martin.Uecker_at_[hidden]>
Date: Mon, 26 Apr 2021 17:17:52 +0000
Am Montag, den 26.04.2021, 18:45 +0300 schrieb Ville Voutilainen:
> On Mon, 26 Apr 2021 at 18:37, Uecker, Martin
> <Martin.Uecker_at_[hidden]> wrote:
> > > They compare and copy like normal objects do, and there's no decay in
> > > them.
> > > Their declarations do not require understanding a "spiral rule".
> >
> > What is the "spiral rule"?
> >
> > Do you mean C's strange declarator syntax?
> Yeah. http://c-faq.com/decl/spiral.anderson.html

Ah, right. Thanks! I forgot.

> > This has not much to do with array types: It is the
> > same for functions and can easily be avoided
> > using typeof when it becomes too complex.
> You need a typedef for every function declaration that uses an array as a return
> type. Or, like P1997 suggests, you need to use a trailing return type.

It is the same for functions returning function pointers:

int (*foo1(int a))(int);

int (*foo2(int a))[3];

But it is not something which comes up often and
typedef is then not really too difficult.

typedef int Array3[3];
Array3 *foo2(int a);

Or one uses typeof:

typeof(int[3]) *foo2(int a);

Which could also be a macro:

#define Array(T, N)) typeof(T[N])

Array(int, 3) *foo2(int a);

Trailing return types would also make this regular.

> That's not what I'd call regular.

I agree it is strange syntax and I agree it should
be improved (and a simple macro is completely
sufficient to get regular syntax if necessary).

But all this has not much to do with the type as such.

> > Otherwise I find
> >
> > int a[3][4];
> >
> > a lot easier to read than
> >
> > std::array< std::array< int, 4 >, 3 > a;
> That's why we are adding mdspan.

Assume I want to simple write a function that transposes
a 2D matrix. In C99 it has this simple prototype:

void transpose(int A, int B, double o[B][A], const double i[A][B]);

How do you write this in C++ (23?) ?


Received on 2021-04-26 12:18:09