cplusplus / CWG

Core Working Group
23 stars 7 forks source link

[expr.type.conv] lacks the wording to specify the number of elements of an unknown bound array type #509

Open xmh0511 opened 4 months ago

xmh0511 commented 4 months ago

Full name of submitter (unless configured in github; will be published with the issue): Jim X

Consider this case:

int main(){
   using type = int[];
   type{1,2,3};
}

[dcl.array] p7 says:

An array bound may also be omitted when an object (but not a non-static data member) of array type is initialized and the declarator is followed by an initializer ([dcl.init], [class.mem], [expr.type.conv], [expr.new]).

The wording does not apply to this example because there is no declaration of an object, the expression is a prvalue. [dcl.init], [expr.new] specify how the array bound is calculated

[dcl.init.aggr] p10

An array of unknown bound initialized with a brace-enclosed initializer-list containing n initializer-clauses is defined as having n elements ([dcl.array]).

[expr.new] p7

If the type-id or new-type-id denotes an array type of unknown bound ([dcl.array]), the new-initializer shall not be omitted; the allocated object is an array with n elements, where n is determined from the number of initial elements supplied in the new-initializer ([dcl.init.aggr], [dcl.init.string]).

However, the nominated clause [expr.type.conv] in [dcl.array] does not describe how the array bound is calculated.

Suggested Resolution

Appending a similar wording in [expr.type.conv] p2

If the specified type is an array of an unknown bound, the initializer shall not be an empty braced-init-list {}, and the bound is determined by the number of elements of the initializer.

t3nsor commented 4 months ago

I don't really see a defect here. If the expression is a prvalue, then the rule applies to the object that the prvalue tells you how to initialize.

However, I'm not opposed to consolidating all the relevant wording in [dcl.array] and making it more precise, if someone volunteers to do that.

t3nsor commented 4 months ago

Incidentally, if someone does volunteer to do that, they might be able to tackle CWG1304 at the same time.

frederick-vs-ja commented 3 months ago

We have specified how to calculate the bound in [dcl.array] p7. I don't think the rule should be repeated for 3 times.

It seems that the "declarator is followed by an initializer" part is problematic, as we don't want to limit the application of this rule to declarators.

How about just saying "is initialized with an initializer" in [dcl.array] p7?