Incomprehensible function signature - Return reference to an array of N objects

前端 未结 2 1932
我在风中等你
我在风中等你 2021-02-05 04:15

I\'ve come across the following signature

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

In the comments it \"claims\" to accept and

2条回答
  •  无人共我
    2021-02-05 05:00

    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.

提交回复
热议问题