General rules of passing/returning reference of array (not pointer) to/from a function?

后端 未结 6 1240
猫巷女王i
猫巷女王i 2020-12-02 06:58

We can pass reference of an array to a function like:

void f(int (&a)[5]);

int x[5];
f(x);     //okay
int y[6];
f(y);     //error - type of y is not `in         


        
6条回答
  •  时光说笑
    2020-12-02 07:12

    You cannot return an array from a function.

    8.3.5/6:

    Functions shall not have a return type of type array or function, although they may have a return type of type pointer or reference to such things.

    EDIT: You'll love the syntax:

    int (&bar()) [5] {
      static int x[5];
      return x;
    }
    
    
    int (* & bar()) [6][10] {
        static int x[6][10];
        static int (*y)[6][10] = &x;
        return y;
    }
    // Note - this returns a reference to a pointer to a 2d array, not exactly what you wanted.
    

提交回复
热议问题