From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 444943858004; Sat, 15 May 2021 21:15:50 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 444943858004 From: "redi at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug libstdc++/100612] std::jthread can't be initialized with a pointer to a member function taking a std::stop_token Date: Sat, 15 May 2021 21:15:50 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: libstdc++ X-Bugzilla-Version: 10.2.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: enhancement X-Bugzilla-Who: redi at gcc dot gnu.org X-Bugzilla-Status: RESOLVED X-Bugzilla-Resolution: INVALID X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: resolution bug_status bug_severity Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 15 May 2021 21:15:50 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D100612 Jonathan Wakely changed: What |Removed |Added ---------------------------------------------------------------------------- Resolution|--- |INVALID Status|UNCONFIRMED |RESOLVED Severity|normal |enhancement --- Comment #6 from Jonathan Wakely --- (In reply to Jonathan O'Connor from comment #5) > I was afraid you were going to say it's not a bug :-) That's why I reached > out to Nico, who was on the committee, and was one of the people who > proposed jthread. Yes, I know, I was there when we reviewed it and voted it into the standard= :-) > My view, as a user, is that jthread should be a drop in replacement for > thread, with auto-joining and stop_token support. And it is. But "stop_token support" doesn't mean "can drop a stop_token at = any arbitrary position in the arguments list and expect it to work". For it to = work with a pointer-to-member we need to change the specification. > I've just reread the descripton of jthread's constructor here: > https://en.cppreference.com/w/cpp/thread/jthread/jthread > and it does seem to explicitly exclude the behaviour I would like. Right. > So, I have no problem with you closing this as not a bug. > Sorry for wasting your time. I guess, I'll have to write a committee > proposal. Please do. > BTW, thanks for your all your work on g++ and the stdlib++. Much apprecia= ted. It's libstdc++ ;-) FWIW this patch makes your code work, and I think this would make a reasona= ble non-standard extension: diff --git a/libstdc++-v3/include/std/thread b/libstdc++-v3/include/std/thr= ead index 886994c1320..ee4fec4a50c 100644 --- a/libstdc++-v3/include/std/thread +++ b/libstdc++-v3/include/std/thread @@ -99,6 +99,16 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION #ifdef __cpp_lib_jthread +#ifndef __STRICT_ANSI__ + template + constexpr bool __pmf_callable_with_stop_token =3D false; + + template + constexpr bool __pmf_callable_with_stop_token<_Callable, _Obj, _Args= ...> + =3D __and_= >, + is_invocable<_Callable, _Obj, stop_token, _Args...>>::valu= e; +#endif + /// A thread that can be requested to stop and automatically joined. class jthread { @@ -211,6 +221,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION static thread _S_create(stop_source& __ssrc, _Callable&& __f, _Args&&... __args) { +#ifndef __STRICT_ANSI__ + if constexpr (__pmf_callable_with_stop_token<_Callable, _Args...>) + return _S_create2(__ssrc, std::forward<_Callable>(__f), + std::forward<_Args>(__args)...); + else +#endif if constexpr(is_invocable_v, stop_token, decay_t<_Args>...>) return thread{std::forward<_Callable>(__f), __ssrc.get_token(), @@ -226,6 +242,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } } +#ifndef __STRICT_ANSI__ + template + static thread + _S_create2(stop_source& __ssrc, _Callable&& __f, _Obj&& __obj, + _Args&&... __args) + { + return thread{std::forward<_Callable>(__f), std::forward<_Obj>(__ob= j), + __ssrc.get_token(), std::forward<_Args>(__args)...}; + } +#endif + stop_source _M_stop_source; thread _M_thread; }; I think I'll make this change in my own GCC fork, but not the gcc.gnu.org version.=