C++ Logo


Advanced search

Re: [wg14/wg21 liaison] Multidimensional subscript operator

From: Uecker, Martin <Martin.Uecker_at_[hidden]>
Date: Mon, 26 Apr 2021 19:31:59 +0000
Am Montag, den 26.04.2021, 20:57 +0300 schrieb Ville Voutilainen:
> On Mon, 26 Apr 2021 at 20:18, Uecker, Martin
> <Martin.Uecker_at_[hidden]> wrote:

> > 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);
> Pretty please. We are not going to suggest C++ users to do that on all
> the millions of C++ projects;
> we can't realistically standardize a useful set of such typedefs, so
> in order to write a simple
> declaration that looks like
> std::array<int, 3> foo2(int);
> we just.. ..let them write that declaration.
> > Which could also be a macro:
> Oh come on.

Why not? If the syntax is really so much of a problem,
then a macro would solve this in C as well
as the std::function or std::array templates do
in C++.

Of course, in C++ you could also use a template
instead of a macro:

c_array<int, 3> x; // same type as int[3]

(But then you can't have VLAs because templates are too
limited for this, I think.)

> > Trailing return types would also make this regular.
> At the cost of requiring trailing return types to be used. That seems
> like a draconian requirement,
> when in general
> T foo(U);
> will just work.

I assume you meant:

std::array<T, N> foo(U);

> > > 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.
> Well, it has everything to do with the type; how to use the type, specifically.

Then we should change the declarator syntax and not
reinvent the type.

> > 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?) ?
> std::mdspan<int, std::extents<2, std::dynamic_extent>>
> transpose(const std::mdspan<int, std::extents<2, std::dynamic_extent>>& input);
> Sure, fine, maybe we would like to entertain standardizing an extent
> type for 2d-dynamic. :P

Thanks, I will stick to C.

(This is also strictly inferior than the C version because the
information about how the size of the arrays relate has been lost.)


Received on 2021-04-26 14:32:08