]> git.ipfire.org Git - thirdparty/gcc.git/commit
libstdc++: fix compile error when converting std::weak_ptr<T[]>
authorGiuseppe D'Angelo <giuseppe.dangelo@kdab.com>
Mon, 9 Dec 2024 23:56:13 +0000 (00:56 +0100)
committerGiuseppe D'Angelo <giuseppe.dangelo@kdab.com>
Fri, 14 Mar 2025 09:52:07 +0000 (10:52 +0100)
commitdf0e6509bf74421ea68a2e025300bcd6ca63722f
treedad0ffdc61838183ca49d5a26b7c29607cc2ae71
parentadb14c7625178b501389c2d7d7c2feec37da7a19
libstdc++: fix compile error when converting std::weak_ptr<T[]>

A std::weak_ptr<T[]> can be converted to a compatible
std::weak_ptr<U[]>. This is implemented by having suitable converting
constructors to std::weak_ptr which dispatch to the __weak_ptr base
class (implementation detail).

In __weak_ptr<T[]>, lock() is supposed to return a __shared_ptr<T[]>,
not a __shared_ptr<element_type> (that is, __shared_ptr<T>).

Unfortunately the return type of lock() and the type of the returned
__shared_ptr were mismatching and that was causing a compile error: when
converting a __weak_ptr<T[]> to a __weak_ptr<U[]> through __weak_ptr's
converting constructor, the code calls lock(), and that simply fails to
build.

Fix it by removing the usage of element_type inside lock(), and using
_Tp instead.

Note that std::weak_ptr::lock() itself was already correct; the one in
__weak_ptr was faulty (and that is the one called by __weak_ptr's
converting constructors).

libstdc++-v3/ChangeLog:

* include/bits/shared_ptr_base.h (lock): Fixed a compile error
when calling lock() on a weak_ptr<T[]>, by removing an
erroneous usage of element_type from within lock().
* testsuite/20_util/shared_ptr/requirements/explicit_instantiation/1.cc:
Add more tests for array types.
* testsuite/20_util/weak_ptr/requirements/explicit_instantiation/1.cc:
Likewise.
* testsuite/20_util/shared_ptr/requirements/1.cc: New test.
* testsuite/20_util/weak_ptr/requirements/1.cc: New test.
libstdc++-v3/include/bits/shared_ptr_base.h
libstdc++-v3/testsuite/20_util/shared_ptr/requirements/1.cc [new file with mode: 0644]
libstdc++-v3/testsuite/20_util/shared_ptr/requirements/explicit_instantiation/1.cc
libstdc++-v3/testsuite/20_util/weak_ptr/requirements/1.cc [new file with mode: 0644]
libstdc++-v3/testsuite/20_util/weak_ptr/requirements/explicit_instantiation/1.cc