Search code examples
c++arraystypesreturntrailing

Function return an array


I know a function cannot return an array, but this function declaration

auto fun(/*...*/) -> int [2]

can pass the compilation. Why!?


Solution

  • Indeed, a function cannot return a plain array by value.

    A declaration with such return type is ill-formed, and if a compiler accepts it without a warning, then the compiler doesn't strictly conform to the standard. It is possibly a bug - or a language extension.

    It appears that this bug is reproducible in g++-6.1. If you try to define the function that returns an array, you can see this hilarious error message:

    error: cannot convert 'int [2]' to 'int [2]' in return