gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 02:03:07 26 4
gpt4 key购买 nike

让我知道我有两个函数:

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:

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com