Skip to content

Before C++26, std::stop_token can't model the concept stoppable_token #200

@Cra3z

Description

@Cra3z

Since std::stop_token lacks the nested class template callback_type prior to C++26 and therefore fails to satisfy the stoppable_token concept, should we apply special handling to it?
I suggest adding the class template stoppable_token_traits, through which we can retrieve the stop callback type using stoppable_token_traits::callback_type.
add new file include/beman/detail/stop_token_traits.hpp:

namespace beman::execution::detail {
template<typename Token>
struct stoppable_token_traits;

template<typename Token> requires requires {
    typename check_type_alias_exist<Token::template callback_type>;
}
struct stoppable_token_traits<Token> {
    template<typename Fn>
    using callback_type = typename Token::template callback_type<Fn>;
};

template<>
struct stoppable_token_traits<std::stop_token> {
    template<typename Fn>
    using callback_type = std::stop_callback<Fn>;
};
} // namespace beman::execution::detail

modify include/beman/detail/stop_callback_for_t.hpp:

 #ifndef INCLUDED_BEMAN_EXECUTION_DETAIL_STOP_CALLBACK_FOR
 #define INCLUDED_BEMAN_EXECUTION_DETAIL_STOP_CALLBACK_FOR
 
+#include <beman/execution/detail/stoppable_token_traits.h>
 #include <concepts>
 
 // ----------------------------------------------------------------------------
 
 namespace beman::execution {
 template <class Token, class CallbackFun>
-using stop_callback_for_t = typename Token::template callback_type<CallbackFun>;
+using stop_callback_for_t =
+    typename ::beman::execution::detail::stoppable_token_traits<Token>::template callback_type<CallbackFun>;
 }
 
 namespace beman::execution::detail {

modify include/beman/execution/detail/stoppable_token.hpp:

 #ifndef INCLUDED_BEMAN_EXECUTION_DETAIL_STOPPABLE_TOKEN
 #define INCLUDED_BEMAN_EXECUTION_DETAIL_STOPPABLE_TOKEN
 
-#include <beman/execution/detail/check_type_alias_exist.hpp>
+#include <beman/execution/detail/stoppable_token_traits.h>
 #include <concepts>
 
 // ----------------------------------------------------------------------------
 namespace beman::execution {
 template <typename Token>
 concept stoppable_token = requires(const Token& token) {
-    typename ::beman::execution::detail::check_type_alias_exist<Token::template callback_type>;
+    typename ::beman::execution::detail::check_type_alias_exist<
+        ::beman::execution::detail::stoppable_token_traits<Token>::template callback_type>;
     { token.stop_requested() } noexcept -> ::std::same_as<bool>;
     { token.stop_possible() } noexcept -> ::std::same_as<bool>;
     { Token(token) } noexcept;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions