Search code examples
c++boostboost-function

Unresolved overloaded function type when using a template friend function


I have a problem where I'm trying to stuff a friend function of a class template into a boost::function:

#include <boost/function.hpp>

template <int N>
struct Vec{
   friend
   double dot(Vec, Vec){}
};

template class Vec<2>; //Force multiple instantiations
template class Vec<3>;

int main()
{
    boost::function<double  (Vec<2>, Vec<2>)> func = dot;
    double (*func1)(Vec<2>, Vec<2>) = dot;
}

Neither of the two lines in main will compile. For the first one, GCC complains:

error: conversion from '<unresolved overloaded function type>' to non-scalar type 'boost::function<double(Vec<2>, Vec<2>)>' requested

The error for the second line seems even more confusing to me:

error: no matches converting function 'dot' to type 'double (*)(struct Vec<2>, struct Vec<2>)'
testtmpl.C:6:15: error: candidates are: double dot(Vec<2>, Vec<2>)
testtmpl.C:6:15: error:                 double dot(Vec<3>, Vec<3>)

I'm kind of stymied since I don't see why double dot(Vec<2>, Vec<2>) doesn't match.

Any ideas as to what is happening here?


Solution

  • In my understanding, if a friend function is defined in a class without other corresponding declarations, the friend name can be looked-up only through ADL.
    §7.3.1.2/3 says:

    The name of the friend is not found by simple name lookup until a matching declaration is provided in that namespace scope

    The code will be able to be compiled by adding corresponding function declaration
    double dot(Vec<2>, Vec<2>); like here.