https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114248

            Bug ID: 114248
           Summary: invalid "scalar object" error
           Product: gcc
           Version: 12.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: f.heckenb...@fh-soft.de
  Target Milestone: ---

% cat test.cpp  
// #pragma GCC diagnostic ignored "-Wnarrowing"
// #pragma GCC diagnostic warning "-Wnarrowing"

template <unsigned> struct S
{
  S (int, int) { }
};

// template <> using S <-1> = int;

int main ()
{
  S <-1> i { 1, 2 };
}
% g++ test.cpp 
test.cpp: In function 'int main()':
test.cpp:13:8: error: narrowing conversion of '-1' from 'int' to 'unsigned int'
[-Wnarrowing]
   13 |   S <-1> i { 1, 2 };
      |        ^
test.cpp:13:10: error: scalar object 'i' requires one element in initializer
   13 |   S <-1> i { 1, 2 };
      |          ^

The first error is correct, of course, but the second one is not because "i" is
not scalar.

Sure, the declaration of "i" is wrong, but this leaves two possible
conclusions:

- We don't know what "i" is meant to be, so any claim about it is unjustified.

- We see that the type of "i" is meant to be an instance of S which is a
struct, and not scalar. AFAIK, even potential specializations of S cannot
change this fact (cf. the commented-out line which is doubly wrong).

Interestingly, when turning the first error off or into a warning (cf. one of
the commented-out pragmas), the second error disappears as well.

I would have expected that those options/pragmas merely control if the
narrowing problem is reported, and if so, whether it causes the compilation to
fail, but apparently it does influence gcc's representation of "i" afterwards
as well.

But that's just a side note actually -- even with a clearly wrong declaration
of "i" such as

  S <""> i { 1, 2 };

gcc gives the "scalar" error which it shouldn't.

Reply via email to