Suppose you call func(0,0)
. During overload resolution both of these are considered:
template <typename... T>
decltype(foo(declval<T>()...) func(T... args);
template <typename... T>
decltype(bar(declval<T>()...) func(T... args);
substitution is done:
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);
foo
and bar
calls are evaluated, then decltype
'd:
template <typename... T = {int, int}>
int func(int, int);
template <typename... T = {int, int}>
int func(int, int);
notice that these are identical signatures. The compiler complains, you aren't allowed to do this.
How you got to identical signatures is, in a sense, immaterial.
You can write a trait that reads "you can call bar
with these arguments". Suppose you do it.
template<class...Ts>
constexpr bool can_call_bar_with = /* some expression */;
and
template<class...Ts>
constexpr bool can_call_foo_with = /* some expression */;
now we can do this:
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);
and now no matter what T...
you pass to it, you never get two func
; this is because I ensured that SFINAE makes only one signature valid.
To write
template<class...Ts>
constexpr bool can_call_bar_with = /* some expression */;
there is the is_detected
or my can_apply
idiom.
See here.
If you want to ask "which, between foo
and `bar, would be preferred in overload resolution", that is a different and more difficult problem. There is no general way; with a list of signatures, you can do it.
//you'd implement this something like:
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.