I'll propose few solutions which are in my opinion more readable than the extremely direct answer to this question. I'm sure there are C grammar enthusiasts out there and I apologize to them for I have terrible memory and I'm not able to remember those C rules.
Type alias
You can avoid the weird C-based grammar by using a type alias:
struct A {
using data_type = int[10];
data_type data;
data_type& f1() { return data; }
data_type const& f2() const { return data; }
};
Live demo
or with typedef
(for before C++11):
struct A {
typedef int data_type[10];
data_type data;
data_type& f1() { return data; }
data_type const& f2() const { return data; }
};
Live demo
Auto
Since C++14 you can also use auto
return types:
struct A {
int data[10];
auto& f1() { return data; }
auto const& f2() const { return data; }
};
Live demo
Standard array
As of C++11 you can also just use std::array
:
struct A {
using data_type = std::array<int, 10>;
data_type data;
data_type& f1() { return data; }
data_type const& f2() const { return data; }
};
Live demo
and simplify it to:
struct A {
std::array<int, 10> data;
};
Live demo
which is somewhat functionally equivalent but easier on the eyes.