diff mbox series

libstdc++: increment *this instead of this

Message ID 20240518065320.362840-1-tchaikov@gmail.com
State New
Headers show
Series libstdc++: increment *this instead of this | expand

Commit Message

Kefu Chai May 18, 2024, 6:53 a.m. UTC
From: Kefu Chai <kefu.chai@scylladb.com>

in _Grapheme_cluster_view::_Iterator, we implement its post-increment
operator (a++) using its pre-increment opereator (++a). but we use

++this

to call the pre-increment opereator in the implementation of the
post-increment operator. one cannot assign to `this`. both GCC and Clang
error out when compiling this piece of code. like:

<source>:7:9: error: expression is not assignable
    7 |         ++this;
      |         ^ ~~~~

and

<source>:7:11: error: increment of read-only location '(Foo*)this'
    7 |         ++this;
      |           ^~~~
<source>:7:11: error: lvalue required as increment operand

to address this issue, we use ++(*this) instead. please note, we don't
use the post-increment operator of _Grapheme_cluster_view::_Iterator
in libstdc++ yet. and _Grapheme_cluster_view::_Iterator is not a part
of the public interface exposed by the std::format. this class is used
to estimate the width of formatted text, so this piece of code is not
tested by existing test cases at this moment. the build failure surfaced
when building our C++20 project with the libstdc++ shippped by GCC-14
and with Clang-19 (881f20e9), which, according to its release notes:

  Clang now performs semantic analysis for unary operators with dependent
  operands that are known to be of non-class non-enumeration type prior
  to instantiation.

I guess that's why this issue was not identified until now.

libstdc++-v3/ChangeLog:

        * include/bits/unicode.h (enable_borrowed_range): Call ++(*this)
        instead of ++this

Signed-off-by: Kefu Chai <kefu.chai@scylladb.com>
---
 libstdc++-v3/include/bits/unicode.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Jakub Jelinek May 18, 2024, 7:25 a.m. UTC | #1
On Sat, May 18, 2024 at 02:53:20PM +0800, Kefu Chai wrote:
> libstdc++-v3/ChangeLog:
> 
>         * include/bits/unicode.h (enable_borrowed_range): Call ++(*this)
>         instead of ++this

This should be already fixed, see https://gcc.gnu.org/PR115119

	Jakub
Kefu Chai May 18, 2024, 7:42 a.m. UTC | #2
On Sat, May 18, 2024 at 3:25 PM Jakub Jelinek <jakub@redhat.com> wrote:

> On Sat, May 18, 2024 at 02:53:20PM +0800, Kefu Chai wrote:
> > libstdc++-v3/ChangeLog:
> >
> >         * include/bits/unicode.h (enable_borrowed_range): Call ++(*this)
> >         instead of ++this
>
> This should be already fixed, see https://gcc.gnu.org/PR115119


Thanks Jakub. Indeed. The mirror of the gcc source repo I am using has some
latencies.


>
>         Jakub
>
>
diff mbox series

Patch

diff --git a/libstdc++-v3/include/bits/unicode.h b/libstdc++-v3/include/bits/unicode.h
index 46238143fb6..12226927b71 100644
--- a/libstdc++-v3/include/bits/unicode.h
+++ b/libstdc++-v3/include/bits/unicode.h
@@ -802,7 +802,7 @@  inline namespace __v15_1_0
 	operator++(int)
 	{
 	  auto __tmp = *this;
-	  ++this;
+	  ++(*this);
 	  return __tmp;
 	}