Can you figure out what is wrong with the statement below?
GCC error states:
‘type name’ declared as function returning array
#define MACRO(a) (a)[1]
class index {
public:
typedef int index_type[2];
const index_type& operator[](int i) const;
};
int k = 0;
int i = MACRO(index()[k]);
btw: I know what is wrong, I thought it was amusing thing to share. Many thanks to litb, his explanation of previous gotcha helped to solve this error pretty quickly.
In the expanded line:
(index()[k])is interpreted as a cast expression, declaring a function that returns an array of lengthkof index. At least, that’s what it looks like is happening. How gcc is manages to validly interpret the[1]as an expression, I’m not sure.