Need help decoding this typedef

  • A+
Category:Languages

I'm trying to create a reference to an array.
It works this way:

typedef int array_type[100];  int main() {     int a[100];     array_type &e = a;    // This works } 

But then I was trying to remove the typedef, and get the same thing working. Haven't been successful.

int main() {     int a[100];     // int[100] &e = a;    // (1) -> error: brackets are not allowed here; to declare an array, place the brackets after the name     // int &e[100] = a;    // (2) -> error: 'e' declared as array of references of type 'int &' } 

What is wrong with my interpreation of typedef? And how could I remove the typedef, and still get the same functionality.

 


You need to add parentheses to tell that this is a reference to array, but not an array of something. e.g.

int (&e)[100] = a; 

Or use auto or decltype (both since C++11) to make it simpler.

auto& e = a; decltype(a)& e = a; 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: