c++ - 如何根据模板参数包是否与函数参数匹配来控制模板函数定义?

标签 c++ overloading variadic-functions function-parameter function-templates

假设我有两个函数:

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);

请注意,这些是相同的签名。编译器会提示,你不能这样做。

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

可以编写一个特征,内容为“您可以使用这些参数调用bar”。假设你这样做。

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 习惯用法。 请参阅here .


如果您想问“在重载解析中,foo 和 `bar 之间哪个更受青睐”,这是一个不同且更困难的问题。没有通用的方法;有了签名列表,你就可以做到。

//你可以像这样实现:

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 .

关于c++ - 如何根据模板参数包是否与函数参数匹配来控制模板函数定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56007776/

相关文章:

c++ - 为什么 CToolBar::LoadToolBar 会失败?

c - 如何判断一个可选参数是否传递给函数 C

c++ - 程序的密码

c++ - 无法编译返回 MPI::Comm 类型对象的函数

c++ - 在 C++ 中永远不会调用重载运算符

c# - 或多或少相等的重载

java - 处理 JNA 回调函数中的 va_list 参数

C 可变参数 - va_copy 问题

c++ - 如果我将一个参数放入默认构造函数中,但给该参数一个默认值,它仍然是一个默认构造函数吗?

C++ lambda 不推断函数重载