简体   繁体   中英

Mixing void_t and variadic templates?

Consider the following code:

template <class F, class... Args, class = std::void_t<>>
struct is_invokable
: std::false_type {};
template <class F, class... Args>
struct is_invokable<F, Args..., std::void_t<std::invoke_result_t<F, Args...>>>
: std::true_type {};

The goal is to have a trait that is able to tell whether a callable of type F is invokable with arguments of type Args... .

However, it fails to compile because:

error: parameter pack 'Args' must be at the end of the template parameter list

What is the (elegant) way to do this in C++17?

namespace details {
  template <class F, class, class... Args>
  struct is_invokable : std::false_type {};
  template <class F, class... Args>
  struct is_invokable<F, std::void_t<std::invoke_result_t<F, Args...>>, Args...>
  : std::true_type {};
}
template <class F, class... Args>
using is_invokable=typename ::details::is_invokable<F, void, Args...>::type;

I propose an helper struct ( is_invokable_h ) and the use of std::tuple to wrap the Args...

Something like

#include <type_traits>
#include <utility>

template <typename, typename, typename = void>
struct is_invokable_h : std::false_type
 {};

template <typename F, typename ... Args>
struct is_invokable_h<F, std::tuple<Args...>,
                      std::void_t<std::invoke_result_t<F, Args...>>>
   : std::true_type
 {};

template <typename F, typename ... Args>
struct is_invokable : is_invokable_h<F, std::tuple<Args...>>
 {};

int foo (int)
 { return 0; }

int main()
 {
   static_assert( true  == is_invokable<decltype(foo), int>{} );
   static_assert( false == is_invokable<decltype(foo), int, int>{} );
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM