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

            Bug ID: 124762
           Summary: GCC should reject (not just warn) narrowing
                    conversions from pointer to bool
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: peppe at gcc dot gnu.org
  Target Milestone: ---

I understand that GCC took the stance that, in general, narrowing conversions
in list initializations from non-constants are accepted, because otherise they
would break too much code:

    char c1 { 42 };     // 1: OK (and legal)
    char c2 { 10'000 }; // 2: ERROR (ill-formed)
    int i =  123;
    char c3 { i };      // 3: WARNING

GCC rejects #2 (narrowing from a constant), but for #3 it limits itself to emit
a diagnostic and accepts the result. (For comparison, Clang rejects the last
one.) This is fully conforming: the code is ill-formed, a diagnostic is issued,
the code is still accepted as a compiler extension.

---

However since C++20 pointer to bool conversions are considered narrowing. I
think that for *those* GCC shouldn't be emitting warnings and accepting, but
unconditionally reject. 

Consider:

    bool b1 { (int *)0 }; // 4: ERROR, narrowing from constant expression
    bool b2 { "test" };   // 5: WARNING¹
    int *ptr = nullptr;
    bool b3 { ptr };      // 6: WARNING


In my opinion all of these should be rejected. There's no actual concrete use
case for allowing pointer->bool narrowing anywhere (unlike, say, int->char).

For pointer->bool the Standard does not even carve out certain constant
expressions, exempting them from being narrowing conversions. They always are.
And GCC already rejects (some of) them.


---


Separate issue: why is #5 just a warning? Is there an issue with constant
folding?

    constexpr const char* p = "test";
    bool b { p };   // WARNING, should be ERROR

    static int x;
    bool c { &x };  // WARNING, should be ERROR

    constexpr int* q = nullptr;
    bool d { q };   // ERROR

Reply via email to