如何根据模板参数包是否匹配函数参数来门控模板函数定义?

How can I gate template function definition based on whether a template parameter pack matches function arguments?

让我知道我有两个功能:

int foo(const int, const float);
int bar(const int, const char);

现在我想根据它是否匹配这些函数之一来重载一个 veradic 模板函数。例如:

template <typename... T>
decltype(foo(declval<T>()...) func(T... args);

template <typename... T>
decltype(bar(declval<T>()...) func(T... args);

但我收到错误消息:

error C2995: 'unknown-type func(T...)': function template has already been defined

因为必须对每个 T 进行不同的定义,所以我会假设这是一个有效的重载,但它似乎不是:(有人可以帮我允许这个重载吗?

假设您调用 func(0,0)。在重载决议期间,这两个都被考虑:

template <typename... T>
decltype(foo(declval<T>()...) func(T... args);

template <typename... T>
decltype(bar(declval<T>()...) func(T... args);

替换完成:

template <typename... T = {int, int}>
decltype(foo(declval<int>(),declval<int>()) func(int,  int);

template <typename... T = {int, int}>
decltype(bar(declval<int>(),declval<int>()) func(int, int);

foobar 调用被评估,然后 decltype'd:

template <typename... T = {int, int}>
int func(int,  int);

template <typename... T = {int, int}>
int func(int, int);

请注意,这些是相同的签名。编译器抱怨,不允许你这样做。

从某种意义上说,如何获得相同的签名并不重要。

可以写一个特征"you can call bar with these arguments"。假设你做到了。

template<class...Ts>
constexpr bool can_call_bar_with = /* some expression */;

template<class...Ts>
constexpr bool can_call_foo_with = /* some expression */;

现在我们可以这样做了:

template <typename... T,
  std::enable_if_t< can_call_foo_with<T...>, bool> = true
>
int func(T... args);

template <typename... T,
  std::enable_if_t< can_call_bar_with<T...> && ! can_call_foo_with<T...>, bool> = true
>
int func(T... args);

现在无论你传给它什么T...,你都不会得到两个func;这是因为我确保 SFINAE 只有一个签名有效。

要写

template<class...Ts>
constexpr bool can_call_bar_with = /* some expression */;

is_detected 或我的 can_apply 成语。 参见


如果你想问"which, between foo and `bar, would be preferred in overload resolution",那是一个不同的更难的问题。没有通用的方法;有了签名列表,你就可以做到。

//你可以这样实现:

template<class...Ts>
struct types_t {};


template<std::size_t I, class Sig>
struct make_tagged_sig;
template<std::size_t I, class Sig>
using tagged_sig = typename make_tagged_sig<I,Sig>::type;

template<std::size_t I, class...Ts>
struct make_tagged_sig<I, types_t<Ts...>> {
  using type=std::integral_constant<std::size_t,I>(Ts...);
};


template<class Sig>
struct overload_check;

template<class R, class...Args>
struct overload_check<R(Args...)> {
  R test(Args...) const;
};

template<class...Sigs>
struct overload_checker:
  overload_check<Sigs>...
{
  using overload_check<Sigs>::test...;

  template<class...Args>
  constexpr auto operator()( types_t<Args...> ) const {
    return decltype( test( std::declval<Args>()... ) ){};
  }
};

template<class Indexes, class...Sigs>
struct which_overload_helper;
template<class...Sigs>
using which_overload_helper_t = typename which_overload_helper<std::index_sequence_for<Sigs...>, Sigs...>::type;
template<std::size_t...Is, class...Sigs>
struct which_overload_helper<std::index_sequence<Is...>, Sigs...> {
    using type = overload_checker< tagged_sig<Is, Sigs>... >;
};

template<class Args, class...Sigs>
constexpr std::size_t which_overload = which_overload_helper_t<Sigs...>{}( Args{} );

Live example.