9

I have a function which can accept any type by universal reference, and would like to overload it for specific types (some of which are templated themselves, although I don't think that's important here). Unfortunately I can't quite seem to to get the overloads to be resolved in the right order.

I would have presumed that the second declaration of foo would be preferred as it's more specific (less templated), although it looks like my understanding of overload resolution is lacking somewhat. Interestingly changing the second declaration to take X by value makes it print "good, good" and making it take X by non-const reference makes it print "bad, good". Obviously removing the first declaration entirely makes it return "good, good", as there's no other choice.

So why does this happen? And most importantly, if the following code doesn't work, how can you overload a function with this signature?

#include <iostream>
#include <string>

class X {};

template<typename T>
inline std::string foo(T && rhs) {
    return "bad";
}

inline std::string foo(const X & rhs) {
    return "good";
}

int main() {
    std::cout << foo(X()) << std::endl;
    X x;
    std::cout << foo(x) << std::endl;
    return 0;
}

Edit:

Maybe a more roundabout solution to this is to do it indirectly. Get rid of the first form of foo and use SFINAE to check if a valid overload exists, it it doesn't then call foo_fallback.

jleahy
  • 16,149
  • 6
  • 47
  • 66

2 Answers2

4

The conversion from X to const X is considered worse than the direct match of the templated overload with T = X or T = X &.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • So how do you make the universal reference version take a backseat to more specific versions? – jleahy May 22 '13 at 15:01
  • @jleahy: You could make *three* non-templated overloads for `X &`, `X const &` and `X &&` to cover all cases... and yes :-) Edit: [see here](http://ideone.com/KNqnRB). – Kerrek SB May 22 '13 at 15:29
  • @jleahy: Or, if you *only* want to exclude `X`, use `enable_if` in the template (and `decay`). – Kerrek SB May 22 '13 at 15:32
  • @jleahy I assume from the question that you have more types than only X, right? I am working on an answer for those cases. Stay tuned ;) – Arne Mertz May 22 '13 at 15:37
  • @Arne Mertz Yes, the idea was that X&& would be a generic fallback. – jleahy May 22 '13 at 15:39
4

To answer your question.comment to Kerre's answer, you could try to use SFINAE:

#include <type_traits>
#include <string>

template <class T>
struct HasFooImpl_ {
  template <typename C>
  static std::true_type test(decltype(fooImpl(std::declval<C>()))*);
  template <typename C> 
  static std::false_type test(...);
  typedef decltype(test<T>(0)) type;
};

template <typename T>
using HasFooImpl = typename HasFooImpl_<T>::type;

template <typename T>
typename std::enable_if<HasFooImpl<T>::value, std::string>::type 
foo(T&& t)
{
  return fooImpl(std::forward<T>(t));
}

template <typename T>
typename std::enable_if<!HasFooImpl<T>::value, std::string>::type
foo(T&& t)
{
    return "generic!";
}

You'd have to implement a function fooImpl for any type that you don't want to be handled genericly.

The implementation was a bit tricky, I tried just enable_if<is_same<string, decltype(fooImpl(declval<C>()))>::value first, but for the fallback the !is_same<>::value gave me compiler errors, because it tried to instantiate the decltype as well.

This implementation has one caveat that you might or might not want to use: if T is convertible to some other type that has a fooImpl defined, that conversion will kick in.

You can see the whole thing in action here: http://ideone.com/3Tjtvj

Update: if you don't want to allow type conversions, it actually gets easier:

#include <type_traits>
#include <string>

template <typename T> void fooImpl(T);

template <typename T>
using HasFooImpl = typename std::is_same<std::string, decltype(fooImpl(std::declval<T>()))>;

template <typename T>
typename std::enable_if<HasFooImpl<T>::value, std::string>::type 
foo(T&& t)
{
  return fooImpl(std::forward<T>(t));
}

template <typename T>
typename std::enable_if<!HasFooImpl<T>::value, std::string>::type
foo(T&& t)
{
    return "generic!";
}

See http://ideone.com/miaoop

Arne Mertz
  • 24,171
  • 3
  • 51
  • 90
  • I like this. [Here's](http://ideone.com/vkyScJ) another implementation of the same idea, but using expression SFINAE instead of a helper class and `enable_if` (the downside is that it requires another level of indirection). – jleahy May 22 '13 at 16:17
  • I actually prefer the latter's conversions semantics, and the implementation's very neat. Excellent. – jleahy May 22 '13 at 16:37