Passing an array by reference

后端 未结 5 1161
隐瞒了意图╮
隐瞒了意图╮ 2020-11-22 11:52

How does passing a statically allocated array by reference work?

void foo(int (&myArray)[100])
{
}

int main()
{
    int a[100];
    foo(a);
}

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-22 12:14

    The following creates a generic function, taking an array of any size and of any type by reference:

    template
    void my_func(T (&arr)[S]) {
       // do stuff
    }
    

    play with the code.

提交回复
热议问题