cplusplus / CWG

Core Working Group
23 stars 7 forks source link

CWG2824 [dcl.init.general] Make std::string arr[] = "some string"; ill-formed again #458

Closed ranaanoop closed 3 months ago

ranaanoop commented 8 months ago

Full name of submitter: Anoop Rana

Reference (section label): [dcl.init.general]

Link to reflector thread (if any):

Issue description: The statement std::string arr[] = "some string"; seems to be allowed by the dcl.init.general#16.5. We observe implementation divergence here.

#include <string>

int main()
{ 
         std::string arr[] = "some string"; //msvc accepts but clang and gcc rejects(as expected) 
} 

In C++ 17 this was not allowed as per dcl.init#17.5 which used to say "Otherwise, if the destination type is an array, the program is ill-formed. " but in C++20 this was changed to dcl.init.general#16.5 to say:

Otherwise, if the destination type is an array, the object is initialized as follows.. Let x1, …, xk be the elements of the expression-list. If the destination type is an array of unknown bound, it is defined as having k elements.. Let n denote the array size after this potential adjustment.. If k is greater than n, the program is ill-formed.. Otherwise, the ith array element is copy-initialized with xi for each 1  ≤ i  ≤ k, and value-initialized for each k<i≤n.. For each 1≤i<j≤n, every value computation and side effect associated with the initialization of the ith element of the array is sequenced before those associated with the initialization of the jth element.

Suggested resolution:

This should be made ill-formed again. Change dcl.init.general#16.5 as indicated:

Otherwise, if the destination type is an array and the initialization is direct-initialization, the object is initialized as follows.. Let x1, …, xk be the elements of the expression-list. If the destination type is an array of unknown bound, it is defined as having k elements.. Let n denote the array size after this potential adjustment.. If k is greater than n, the program is ill-formed.. Otherwise, the ith array element is copy-initialized with xi for each 1  ≤ i  ≤ k, and value-initialized for each k<i≤n.. For each 1≤i<j≤n, every value computation and side effect associated with the initialization of the ith element of the array is sequenced before those associated with the initialization of the jth element.

Also add a separate bullet point after dcl.init.general#16.5 saying:

Otherwise, if the destination type is an array and the initialization is copy-initialization, the program is ill-formed.

frederick-vs-ja commented 8 months ago

The change was made by P0960R3. Presumably copy-initialization should be left unchanged.

Suggested resolution:

Change [dcl.init.general] p16.5 as indicated:

Otherwise, if the destination type is an array and the initialization is direct-initialization, the object is initialized as follows. [...]

Add a new bullet after [dcl.init.general] p16.5

Otherwise, if the destination type is an array and the initialization is copy-initialization, the program is ill-formed.

ranaanoop commented 8 months ago

The change was made by P0960R3.

I see, before posting this issue I was trying to find the reason(thing that caused this change) why this was changed in the diff section of the draft but could not find it there. Now I see that it was [P0960R3] that introduced the change.

Thanks @frederick-vs-ja , I've incorporated your suggestion in the issue's "suggested resolution" part at the end of this issue.

jensmaurer commented 8 months ago

CWG2824