3

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?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Gretchen
  • 2,274
  • 17
  • 16

2 Answers2

5

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.

Ise Wisteria
  • 11,259
  • 2
  • 43
  • 26
  • 1
    +1 This makes sense. I was thinking on this last night and I discarded for some reason, but it does actually make sense. Since the original code is already doing explicit instantiations, having to *declare* all the functions should not be a problem. Another alternative is creating a templated free function of static member function that will perform the dispatch (`dot` will be internally accessible through ADL). – David Rodríguez - dribeas Jul 15 '11 at 07:26
  • Indeed, I suppose this friend name look-up is one of the most confusing rules in C++. I tore out my head when I first heard this explanation :) – Ise Wisteria Jul 15 '11 at 08:28
  • As any popular C++ author/guru ever recommended to always declare a function prior to making it a friend? (I seem to recall something about function *template* friends.) This is a rule of thumb of mine but I have no idea if it solves a lot of problems. – Luc Danton Jul 15 '11 at 10:35
0

This will work:

template <int N> struct Vec;

template <int K> double dot(Vec<K>, Vec<K>) { return 0; }

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

GCC 4.6.1 actually gave a very helpful warning:

warning: friend declaration ‘double dot(Vec<N>, Vec<N>)’ declares a non-template function [-Wnon-template-friend]
note: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here)
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • The semantics of this piece of code are slightly different than the original. If there are implicit conversions from *anything* to `Vec` those will not be applied, since overload resolution for the template requires *perfect* matching of the arguments. – David Rodríguez - dribeas Jul 14 '11 at 21:23
  • Well, kind of... it defines a friend function for each instantiation of the template, the function is defined inline... that is a fairly common piece of code. Think that the friend function might be called `operator<<` for one common example. – David Rodríguez - dribeas Jul 14 '11 at 21:29