Boost logo

Boost :

Subject: Re: [boost] SafeInt code proposal
From: Niels Dekker - address until 2010-10-10 (niels_address_until_2010-10-10_at_[hidden])
Date: 2009-06-22 16:40:05


Steven Watanabe wrote:
> I don't think that non-template friends should be defined inline in a
> class template because they are always supposed to be instantiated
> regardless of whether they are used or not (unlike ordinary members).

Thanks, Steven. Do I understand correctly that when you look at the
following example, you find the definition of operator+ preferable to
operator- ?

   //////////////////////////////////////////////////////////
   template <typename> class safe_int;

   template <typename T>
   safe_int<T> operator+(safe_int<T>, safe_int<T>);

   template <typename T> class safe_int
   {
   public:
     friend safe_int operator+ <>(safe_int, safe_int);

     inline friend safe_int operator-(safe_int, safe_int)
     {
       throw "operator- not yet implemented.";
     }
   };

   template <typename T>
   safe_int<T> operator+(safe_int<T>, safe_int<T>)
   {
     throw "operator+ not yet implemented.";
   }
   //////////////////////////////////////////////////////////

I still hesitate, because the non-template inline friend, operator-,
appears to support implicit conversions more intuitively... I think.

   //////////////////////////////////////////////////////////
   struct convertible_to_safe_int
   {
     operator safe_int<int>() const
     {
       return safe_int<int>();
     }
   };

   int main()
   {
     safe_int<int>() - convertible_to_safe_int(); // okay
     convertible_to_safe_int() - safe_int<int>(); // okay
     safe_int<int>() + convertible_to_safe_int(); // error
     convertible_to_safe_int() + safe_int<int>(); // error
   }
   //////////////////////////////////////////////////////////

Isn't support of such conversions (in a symmetrical way) the main reason
to declare such operators as non-members, rather than as member functions?

Kind regards, Niels

-- 
Niels Dekker
http://www.xs4all.nl/~nd/dekkerware
Scientific programmer at LKEB, Leiden University Medical Center

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk