Boost logo

Boost :

From: Howard Hinnant (hinnant_at_[hidden])
Date: 2000-06-11 11:25:09

David Abrahams wrote on 6/10/2000 11:51 PM
>My current theory about this is that this is that the barton&nackmann trick
>employed by operators.hpp causes the global (operator) functions in the base
>class to be instantiated regardless of whether they are used or not. Since
>these are not template functions or member functions of a class template I
>also theorize that the compiler is correct in doing so. I think
>wrapped_iterator probably needs to give up on using the operators templates
>for that reason.

I believe you are correct:

14.5.3 / 5:

-5- When a function is defined in a friend function declaration in a
class template, the function is defined at each instantiation of the
class template. The function is defined even if it is never used. The
same restrictions on multiple declarations and definitions which apply to
non-template function declarations and definitions also apply to these
implicit definitions. [Note: if the function definition is ill-formed for
a given specialization of the enclosing class template, the program is
ill-formed even if the function is never used. ]

I'm not positive the following workaround doesn't have a gotcha, but I
think it is worth exploring:

template <class T, class U, class D = boost::detail::empty_base>
struct addable2 : D
     friend T operator+<>( T x, const U& y );
     friend T operator+<>( const U& y, T x );

template <class T, class U>
operator+( T x, const U& y )
        return x += y;

template <class T, class U>
operator+( const U& y, T x )
        return x += y;

I can't claim to completely understand friends of templates (but I'm
learning all the time!). For a humbling experience try reading (and
completely understanding):


Boost list run by bdawes at, gregod at, cpdaniel at, john at