]> git.ipfire.org Git - thirdparty/gcc.git/commit
libstdc++: Add static_assert to std::packaged_task::packaged_task(F&&)
authorJonathan Wakely <jwakely@redhat.com>
Mon, 10 Mar 2025 14:29:36 +0000 (14:29 +0000)
committerJonathan Wakely <redi@gcc.gnu.org>
Wed, 12 Mar 2025 17:02:12 +0000 (17:02 +0000)
commit4d2683b04fd329c97e3da09498345fe3ee00455f
treea17142ac9e1914ebecd58cb7dc525acb735943f5
parent0ce4c1c48564e465a331c100e757e2258b4c632a
libstdc++: Add static_assert to std::packaged_task::packaged_task(F&&)

LWG 4154 (approved in Wrocław, November 2024) fixed the Mandates:
precondition for std::packaged_task::packaged_task(F&&) to match what
the implementation actually requires. We already gave a diagnostic in
the right cases as required by the issue resolution, so strictly
speaking we don't need to do anything. But the current diagnostic comes
from inside the implementation of std::__invoke_r and could be more
user-friendly.

For C++17 (when std::is_invocable_r_v is available) add a static_assert
to the constructor, so the error is clear:

.../include/c++/15.0.1/future: In instantiation of 'std::packaged_task<_Res(_ArgTypes ...)>::packaged_task(_Fn&&) [with _Fn = const F&; <template-parameter-2-2> = void; _Res = void; _ArgTypes = {}]':
lwg4154_neg.cc:15:31:   required from here
   15 | std::packaged_task<void()> p(f); // { dg-error "here" "" { target c++17 } }
      |                               ^
.../include/c++/15.0.1/future:1575:25: error: static assertion failed
 1575 |           static_assert(is_invocable_r_v<_Res, decay_t<_Fn>&, _ArgTypes...>);
      |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Also add a test to confirm we get a diagnostic as the standard requires.

libstdc++-v3/ChangeLog:

* include/std/future (packaged_task::packaged_task(F&&)): Add
static_assert.
* testsuite/30_threads/packaged_task/cons/dangling_ref.cc: Add
dg-error for new static assertion.
* testsuite/30_threads/packaged_task/cons/lwg4154_neg.cc: New
test.

Reviewed-by: Tomasz Kamiński <tkaminsk@redhat.com>
libstdc++-v3/include/std/future
libstdc++-v3/testsuite/30_threads/packaged_task/cons/dangling_ref.cc
libstdc++-v3/testsuite/30_threads/packaged_task/cons/lwg4154_neg.cc [new file with mode: 0644]