Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I need some help regarding §8/5 in the spec

§8/5:

The optional attribute-specifier-seq in a trailing-return-type appertains to the indicated return type. The type-id in a trailing-return-type includes the longest possible sequence of abstract-declarators. [ Note: This resolves the ambiguous binding of array and function declarators. [ Example:

auto f()->int(*)[4]; // function returning a pointer to array[4] of int
                     // not function returning array[4] of pointer to int

—end example ] —end note ]

The "type-id in a trailing-return-type" doesn't make sense to me, simply because a trailing-return-type doesn't contain a type-id according to the grammar.

I also don't understand the "ambiguous binding" of array and function declaration. As far as I can understand

auto f() -> int*[4]; // function returning an array of 4 pointers to int
auto f() -> int(*)[4]; // function returning a pointer to an array of 4 ints  
like image 994
Leon Avatar asked Apr 06 '15 21:04

Leon


People also ask

What is the length of an iPhone 8 in inches?

The most obvious difference is the size of the phones, with iPhone 8 measuring in at 4.7-inches and iPhone 8 Plus at 5.5.

Does iPhone 8 have optical zoom?

Like the iPhone 7, iPhone 8 will feature a 7-megapixel front-facing FaceTime camera and a 12-megapixel rear-facing camera with an f/1.8 aperture. The rear camera system also has optical image stabilization and a 5X digital zoom.


1 Answers

int *f();

Declares a function of () returning pointer to int.

int *f()[4];

Declares a function of () returning array of 4 pointers to int. Note that this is ill-formed.

int (*f())[4];

Declares a function of () returning pointer to array of 4 int.

Now, in

  auto f() -> int(*)[4]
//     ^^^^^^^^^^^^^---

What the rule resolves is whether [4] is part of the trailing-return-type, and hence part of the the function declarator. If [4] is part of the trailing-return-type, then the above declaration declares a function of () returning pointer to array of 4 int.

If not, then [4] would form an array declarator that is not part of the function declarator, and the parse would be governed by [dcl.array]/p1:

In a declaration T D where D has the form

D1 [ constant-expression_opt ] attribute-specifier-seq_opt

and the type of the identifier in the declaration T D1 is “derived-declarator-type-list T” [..., if] the value of the constant expression is N, [...] the type of the identifier of D is “derived-declarator-type-list array of N T”.

and since auto f()-> int (*) declares f as "function of () returning pointer to int", substitution tells us that this would declare a function returning an array of 4 pointers to int, just like int *f()[4];.

like image 52
T.C. Avatar answered Sep 19 '22 12:09

T.C.