alias for variable of array one and/or two dimension

How to make alias for variable of a one and/or two dimensional array
as alias of int:

1
2
int k;
int& k_ = k;

So how
 
int a[70];   // how then declaration/definition of array a alias a_ ...? 
Last edited on
Like so?
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

int main(){
  int a;
  int &a_ = a;
  int b[10];
  int (&b_)[10] = b;
  cout << sizeof(a) << endl;
  cout << sizeof(a_) << endl;
  cout << sizeof(b) << endl;
  cout << sizeof(b_) << endl;
}
It's good to know the actual syntax, but it can of course be done more easily:

 
  auto& b_ = b;
> How to make alias for variable of a one and/or two dimensional array

Do it in a series of simple steps. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{
    using arr_t = int[23] ; // type alias for array of 23 int
    typedef int arr_t[23] ; // same as above, it is just a redeclaration

    using ref_t = arr_t& ; // type alias for reference to array of 23 int

    arr_t a1 {} ; // same as int a1[23]{} ;
    ref_t r = a1 ; // initialise the reference to refer to a1
    auto& r1 = a1 ; // simpler: use type deduction

    using arr2d_t = arr_t[5] ; // type alias for array of 5 arr_t ie. 2d array int[5][23]
    using ref2d_t = arr2d_t& ; // type alias for reference to 2d array int[5][23]

    arr2d_t a2d {} ; // same as int a1[5][23]{} ;
    ref2d_t r2d = a2d ; // initialise the reference to refer to a2d
    auto& r2d1 = a2d ; // simpler: use type deduction
}
Topic archived. No new replies allowed.