How overload resolution works
First we do name lookup and template type deduction. For foo(d)
, this gives us two options:
foo(Derived const&)
, with [C = Derived]
foo(Base const&)
These are our viable candidates.
Then we determine which one of the overloads is the best viable candidate. This is done first by looking at conversion sequences. In this case, (1) is an Exact Match whereas (2) involves a derived-to-base conversion which has Conversion rank (see this table). Since that ranks worse, candidate (1) is preferred and is thus deemed the best viable candidate.
How to do what you really want
The simplest way is to simply add a third overload for Derived
:
foo(Derived const&)
Here, both (1) and (3) would be equivalent in terms of conversion sequence. But functions that aren't templates are preferred to functions that are templates (think of it as picking the most specific option), so (3) would be selected.
But you don't want to do that. So the options are either: make (1) not work for Derived
or make (2) work for Derived
too in a way that's preferred.
Disable the general template
We can use SFINAE to simply exclude anything derived from Base
:
template <class T, class = std::enable_if_t<!std::is_convertible<T*, Base*>::value>
void foo(T const& ); // new (1)
void foo(Base const& ); // same (2)
Now, (1)
is no longer a viable candidate for the match, so (2) is trivally preferred.
Redo the overloads so that the Base is preferred
Taking a tip out of Xeo's book, we can restructure the overloads thusly:
template <int I> struct choice : choice<I + 1> { };
template <> struct choice<10> { };
struct otherwise { otherwise(...) {} };
template <class T> void foo(T const& val) { foo_impl(val, choice<0>{}); }
And now we can prefer those types derived from Base
:
template <class T, class = std::enable_if_t<std::is_convertible<T*, Base*>::value>>
void foo_impl(T const& val, choice<0> ); // new (2)
template <class T>
void foo_impl(T const& val, otherwise ); // new (1)
This changes the mechanics of how overload resolution works and is worth going through into the separate cases.
Calling foo(d)
means we're calling foo_impl(d, choice<0> )
and gives us two viable candidates:
foo_impl(Derived const&, choice<0> )
, with [T = Derived]
foo_impl(Derived const&, otherwise )
, with [T = Derived]
Here, the first argument is identical, but for the second argument, the first overload is an Exact Match while the second argument requires a Conversion via a variadic argument. otherwise
will always be the word choice as a result, so the first overload is preferred. Exactly what we want.
Calling foo(not_a_base)
, on the other hand, would only give us one viable candidate:
foo_impl(NotABase const&, otherwise )
, with [T = NotABase]
The other one was removed due to the deduction failure. So this is trivially the best viable candidate, and again we get exactly what we want.
For your specific question, I'd write something like:
template <class T>
size_t n_items(T const& cont) { return n_items(cont, choice<0>{}); }
with:
// has count?
template <class T>
auto n_items(T const& cont, choice<0> ) -> decltype(cont.count()) {
return cont.count();
}
// else, has size?
template <class T>
auto n_items(T const& cont, choice<1> ) -> decltype(cont.size()) {
return cont.size();
}
// else, use static_size
template <class T>
size_t n_items(T const& cont, otherwise )
return static_size(cont);
}
Base
function acceptable, is it preferable? Is your template really a "catch everything except everything derived fromBase
? Wish you would clarify some more.n_items( o )
for all kinds of objecto
, includingstd::vector
(where it invokessize
member),std::bitset
(where it invokescount
, I think it was), raw arrays, and my own string classes, where one is derived from a more general one. Then_items
template is specialized for bitset and raw array, and has a generic implementation as a catch-all for types that don't define their own overloads.template<typename R, typename F> struct UserConv { R r; template<typename U> UserConv(U &&u):r(F::call(u)) { } };
which allows you to saystruct SizeCall { template<typename T> static size_t call(const T& t) { return t.size(); } };
and usesize_t foo(UserConv<size_t, SizeCall> uc) { return uc.r; }
. This overload has user-defined conversion cost and is always second-choice against derived-to-base conversion. All it needs is some SFINAE protection againstsize()
not being found.