Bug 99625 - GCC does not detect narrowing in aggregate initialization
Summary: GCC does not detect narrowing in aggregate initialization
Status: ASSIGNED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 10.2.1
: P3 normal
Target Milestone: ---
Assignee: Marek Polacek
URL:
Keywords: accepts-invalid
Depends on:
Blocks:
 
Reported: 2021-03-17 13:42 UTC by Giuseppe D'Angelo
Modified: 2024-11-23 03:09 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2024-02-01 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Giuseppe D'Angelo 2021-03-17 13:42:05 UTC
Testcase:

    extern long double l;

    struct S {
        S(int) {}
    };

    S s[] = { l };


This is supposed to be ill-formed because of narrowing. GCC does not reject the code, nor it warns (as it usually does for narrowing).

The relevant rule is in [dcl.init.aggr]:

https://eel.is/c++draft/dcl.init.aggr#4.2.sentence-2

> Otherwise, the element is copy-initialized from the corresponding initializer-clause or is initialized with the brace-or-equal-initializer of the corresponding designated-initializer-clause.
> If that initializer is of the form assignment-expression or = assignment-expression and a narrowing conversion ([dcl.init.list]) is required to convert the expression, the program is ill-formed.


That rule has been changed in C++20 with designated initializers, but the same one was also there for C++17 and before, e.g. https://timsong-cpp.github.io/cppwp/n4659/dcl.init.aggr#3


For comparison, Clang accepts with no warnings, but MSVC warns about narrowing.
Comment 1 Jonathan Wakely 2021-07-22 14:26:23 UTC
Confirmed, this should be a -Wnarrowing warning.
Comment 2 Marek Polacek 2021-07-22 14:27:49 UTC
I've dealt with this in the past -- mine.
Comment 3 Giuseppe D'Angelo 2022-11-15 14:39:49 UTC
Hi,

Sorry to ping, but some time has gone by -- I guess this fell through the cracks?
Comment 4 Marek Polacek 2022-11-15 14:45:58 UTC
(In reply to Giuseppe D'Angelo from comment #3)
> Hi,
> 
> Sorry to ping, but some time has gone by -- I guess this fell through the
> cracks?

Yes, it did.  Sorry.  :-(
Comment 5 Giuseppe D'Angelo 2022-11-15 14:51:31 UTC
No problem, thanks for working on GCC :)