Channel72
Channel72

Reputation: 24719

Why doesn't overloaded function bind to more specific overload?

Consider the following overloaded functions:

template <class T>
void foo(const T& v)
{
    std::cout << "Generic version" << std::endl;
}

void foo(std::pair<const void*, std::size_t> p)
{
    std::cout << "Pair version" << std::endl;
}

Below, I expect the second overload (the one that takes an std::pair) to be called:

int main()
{
    const void* buf = 0;
    std::size_t sz = 0;
    foo(std::make_pair(buf, sz));
}

However, this code in fact calls the generic version. Why doesn't it bind to the overload that specifically takes an std::pair? Is this a compiler bug? I'm using a pretty old compiler, GCC 4.1.2

Upvotes: 1

Views: 83

Answers (1)

Alexander L. Belikoff
Alexander L. Belikoff

Reputation: 5721

  • You need to declare your specialized function as a template
  • Your specialized argument type must follow the template parameter (i.e. be a const reference) as well.

Try

template <>
void foo(const std::pair<const void*, std::size_t>& p)
{
    ...
}

Upvotes: 3

Related Questions