microsoft / GSL

Guidelines Support Library
Other
6.14k stars 738 forks source link

How do we use gsl::at with multi-dimension arrays? #1002

Closed ajtruckle closed 2 years ago

ajtruckle commented 2 years ago

At the moment I have:

if( aryUIntSynchData[iPTS][iCol])

Up until now the arrays I have been porting are one dimension, e.g:

if(gsl::at(array, index))

How do we write this for a two dimensional array?

beinhaerter commented 2 years ago

You can write gsl::at(gsl::at(aryUIntSynchData, iPTS), iCol).

ajtruckle commented 2 years ago

It does not like that: Error2

ajtruckle commented 2 years ago

It would be nice if we could simply do:

if(gsl::at(aryUIntSynchData, iPTS, iCol))

beinhaerter commented 2 years ago

The code works for raw arrays:

#include <gsl/gsl>

void f()
{
    int aryUIntSynchData[14][14]{};
    int iPTS{}, iCol{};
    if (aryUIntSynchData[iPTS][iCol]) {}
    if (gsl::at(gsl::at(aryUIntSynchData, iPTS), iCol)) {}
 }

If you use MFC's CUIntArray you should have mentioned this in the first place. It is a relevant part of the question. So: what is the exact type of aryUIntSynchData?

You can try gsl::at(aryUIntSynchData, iPTS)[iCol].

ajtruckle commented 2 years ago

@beinhaerter I am really sorry that I overlooked that important clarification!

what is the exact type of aryUIntSynchData?

It is: CUIntArray aryUIntSynchData[to_underlying(CPTSDatabase::Sync::Count)]; I just tried your suggestion and I get no warnings. Thanks.