public inbox for libstdc++-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jonathan Wakely <redi@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org
Subject: [gcc r14-1573] libstdc++: Fix ambiguous expression in std::array<T, 0>::front() [PR110139]
Date: Tue,  6 Jun 2023 11:45:47 +0000 (GMT)	[thread overview]
Message-ID: <20230606114547.E0B4B3858002@sourceware.org> (raw)

https://gcc.gnu.org/g:56001fad4ecc32396beead6644906e3846244b67

commit r14-1573-g56001fad4ecc32396beead6644906e3846244b67
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Tue Jun 6 11:38:42 2023 +0100

    libstdc++: Fix ambiguous expression in std::array<T, 0>::front() [PR110139]
    
    For 32-bit targets using -pedantic (or using Clang) makes the expression
    _M_elems[0] ambiguous.  The overloaded operator[] that we want to call
    has a size_t parameter, but 0 is type ptrdiff_t for many ILP32 targets,
    so using the implicit conversion from _M_elems to T* and then
    subscripting that is also viable.
    
    Change the 0 to (size_type)0 and also make the conversion to T*
    explicit, so that's it's not viable here. The latter change requires a
    static_cast in data() where we really do want to convert _M_elems to a
    pointer.
    
    libstdc++-v3/ChangeLog:
    
            PR libstdc++/110139
            * include/std/array (__array_traits<T, 0>::operator T*()): Make
            conversion operator explicit.
            (array::front): Use size_type as subscript operand.
            (array::data): Use static_cast to make conversion explicit.
            * testsuite/23_containers/array/element_access/110139.cc: New
            test.

Diff:
---
 libstdc++-v3/include/std/array                                 | 10 +++++-----
 .../testsuite/23_containers/array/element_access/110139.cc     |  5 +++++
 2 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/libstdc++-v3/include/std/array b/libstdc++-v3/include/std/array
index 27354d9e3fb..70280c1beeb 100644
--- a/libstdc++-v3/include/std/array
+++ b/libstdc++-v3/include/std/array
@@ -69,7 +69,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
        // Conversion to a pointer produces a null pointer.
        __attribute__((__always_inline__))
-       constexpr operator _Tp*() const noexcept { return nullptr; }
+       constexpr explicit operator _Tp*() const noexcept { return nullptr; }
      };
 
      using _Is_swappable = true_type;
@@ -240,7 +240,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       front() noexcept
       {
 	__glibcxx_requires_nonempty();
-	return _M_elems[0];
+	return _M_elems[(size_type)0];
       }
 
       [[__nodiscard__]]
@@ -250,7 +250,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 #if __cplusplus >= 201402L
 	__glibcxx_requires_nonempty();
 #endif
-	return _M_elems[0];
+	return _M_elems[(size_type)0];
       }
 
       [[__nodiscard__]]
@@ -274,12 +274,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       [[__nodiscard__, __gnu__::__const__, __gnu__::__always_inline__]]
       _GLIBCXX17_CONSTEXPR pointer
       data() noexcept
-      { return _M_elems; }
+      { return static_cast<pointer>(_M_elems); }
 
       [[__nodiscard__]]
       _GLIBCXX17_CONSTEXPR const_pointer
       data() const noexcept
-      { return _M_elems; }
+      { return static_cast<const_pointer>(_M_elems); }
     };
 
 #if __cpp_deduction_guides >= 201606
diff --git a/libstdc++-v3/testsuite/23_containers/array/element_access/110139.cc b/libstdc++-v3/testsuite/23_containers/array/element_access/110139.cc
new file mode 100644
index 00000000000..6f09aacf0a7
--- /dev/null
+++ b/libstdc++-v3/testsuite/23_containers/array/element_access/110139.cc
@@ -0,0 +1,5 @@
+// { dg-options "-pedantic" }
+// { dg-do compile { target c++11 } }
+#include <array>
+template class std::array<int, 0>;
+// { dg-bogus "ambiguous overload" "" { target *-*-* } 0 }

                 reply	other threads:[~2023-06-06 11:45 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230606114547.E0B4B3858002@sourceware.org \
    --to=redi@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    --cc=libstdc++-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).