25

I've come across the following signature

double(&rotate_vec(double(&val)[4]))[4];

In the comments it "claims" to accept and return an array of four elements. My first reaction was that this does not even look standard c++ yet this compiles:

double(&rotate_vec(double(&val)[4]))[4]
{
    // ... 
    return val;
}

int main()
{
    double ar[4] = { 1, 2, 3, 5 };
    rotate_vec(ar);
    return 0;
}
  1. How is this c++ ? How would you read it ?
  2. We can't return an array from a function, just pointers, or can we ?
Community
  • 1
  • 1
Nikos Athanasiou
  • 29,616
  • 15
  • 87
  • 153
  • 2
    1. for the function, the argument part is `double(&val)[4]`, declaring `val` is a reference, to an array with 4 `double`s. The function itself returns a reference to an array with 4 `double`s. – xis May 01 '14 at 20:30
  • 3
    2. We are not returning an array from a function, but a reference to an array. – xis May 01 '14 at 20:30
  • Look at the comments on the "can't return an array from a function" link. Specifically, "But you don't even need the typedef if you care to write this: `int (&foo())[5] { static int a[5] = {}; return a; }`" – Eric Finn May 01 '14 at 20:32

2 Answers2

28

With C++03 the best you can do to simplify the original

double(&rotate_vec(double(&val)[4]))[4];

is to use a typedef, to wit:

typedef double Four_vec[4];
Four_vec& rotate_vec( Four_vec& val );

In C++11 you can write

auto rotate_vec( double (&val)[4] )
    -> double (&)[4];

although I'd use a typedef or C++11 using to clarify.


Regarding

“We can't return an array from a function, just pointers, or can we ?”

you can't return a raw array by value, but you can return a pointer or reference, or you can wrap it in a struct, like C++11 std::array.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • 1
    Nice to see someone picking a good old and very readable typedef over a semi-obscure C++11 solution. It's rare these days :) – Martin G May 02 '14 at 06:24
  • 1
    The typedef isn't readable at all, it obscures what's happening and clutters the namespace. The C++11 solution doesn't obscure anything, it in fact makes it much more readable. – Miles Rout May 10 '14 at 02:40
21
double ( &rotate_vec( double (&val)[4] ) )[4]

A function named rotate_vec

double ( &rotate_vec( double (&val)[4] ) )[4]

...that takes as an argument, a reference to an array of four doubles

double ( &rotate_vec( double (&val)[4] ) )[4]

...and returns a reference to an array of four doubles.

template boy
  • 10,230
  • 8
  • 61
  • 97
  • C++ is not one of 'my' languages, though interested in the syntax. Where did the outer parentheses come from? – 11684 May 02 '14 at 07:37
  • @11684: the syntax for declaring a reference `a` to a raw array is like `double (&a)[4]`. it's pretty arbitrary (both the C creators and the C++ creator has referred to this kind of syntax as a "failed experiment"). anyway, just replace `a` with function head. – Cheers and hth. - Alf May 02 '14 at 09:02
  • @Cheersandhth.-Alf I thought you had to use the parentheses so that it wouldn't be interpreted as an array of references (which is illegal)... – template boy May 02 '14 at 13:58
  • @templateboy: well, as a language designer you can define the syntax to mean whatever you want. bjarne has had a stated preference for *uniform rules*. that means choosing syntax and semantics for new things so that apparently simple rules apply generally (albeit with baffling special cases here and there), which makes it easier to remember, understand and rationalize things. if he had chosen otherwise here, then the parenthesis could have been omitted, at the cost of yet another special case to remember. still, even with uniform rules c++ is one of the most complex programming languages. – Cheers and hth. - Alf May 02 '14 at 19:25
  • @DDrmmr It was hard to read when it was just text before I edited; and what's wrong with using images to show text anyway? – template boy May 20 '14 at 00:49
  • 2
    @templateboy An image cannot be read out loud, will not be displayed using an inverse color scheme, cannot be displayed in a different/larger font, will not be translated, etc. Basically, you are (unintentionally) giving a big FU to anyone slightly out of the ordinary. – D Drmmr May 21 '14 at 10:33
  • @DDrmmr I know I'm a year late but back to text! :) – template boy Jan 25 '15 at 03:52