friend template function with multiple types

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bonzo

    friend template function with multiple types

    I'm trying to write a class for a smart pointer. I'm having a problem
    though when trying to implement operator== with both sides being a Smart.
    (Or any function that I try to bring U into.)
    The pointer contained might not be the exact same type but still get
    tested for equality. Here's what I have:


    template <typename T> class Smart;

    template<typena me T, typename U>
    bool operator== ( const Smart<T>& a, const Smart<U>& b );

    template <typename T> class Smart
    {
    /* On the next line, GCC says:
    "type/value mismatch at argument 1 in template parameter list for
    `template<class T> class Smart expected a type, got `U'"
    */

    friend bool operator== ( const Smart<T>& a, const Smart<U>& b );
    // friend bool operator==<> ( const Smart<T>& a, const Smart<U>& b );

    }

    bool operator== (const Smart<T>& a, const<SmartU>& b)
    {
    return true;
    }


    It also warns about the angle brackets not being there, which are in the
    commented out line, but adding them does not solve the error.

    What must I do to make the compiler happy?

    Thanks,
    -Jim
  • Rob Williscroft

    #2
    Re: friend template function with multiple types

    Bonzo wrote in
    news:dont-C31CFE.15391410 072003@syrcnyrd rs-02-ge0.nyroc.rr.co m:
    [color=blue]
    > I'm trying to write a class for a smart pointer. I'm having a problem
    > though when trying to implement operator== with both sides being a
    > Smart. (Or any function that I try to bring U into.)
    > The pointer contained might not be the exact same type but still get
    > tested for equality. Here's what I have:
    >
    >
    > template <typename T> class Smart;
    >
    > template<typena me T, typename U>
    > bool operator== ( const Smart<T>& a, const Smart<U>& b );
    >
    > template <typename T> class Smart
    > {
    > /* On the next line, GCC says:
    > "type/value mismatch at argument 1 in template parameter list
    > for
    > `template<class T> class Smart expected a type, got `U'"
    > */
    >[/color]
    // Overkill but harmless:
    template <typename A, typename B>
    friend bool operator == (Smart<A> const &a, Smart<B> const &b);


    // But why not just make it a member function?

    template <typename U>
    bool operator == (Smart<U> const &rhs) const
    {
    return false /* or whatever */;
    }

    [color=blue]
    > friend bool operator== ( const Smart<T>& a, const Smart<U>& b );
    > // friend bool operator==<> ( const Smart<T>& a, const Smart<U>& b );
    >
    > }
    >
    > bool operator== (const Smart<T>& a, const<SmartU>& b)
    > {
    > return true;
    > }
    >
    >
    > It also warns about the angle brackets not being there, which are in
    > the commented out line, but adding them does not solve the error.
    >
    > What must I do to make the compiler happy?
    >[/color]

    HTH

    Rob.
    --

    Comment

    • Victor Bazarov

      #3
      Re: friend template function with multiple types

      "Bonzo" <dont@email.m e> wrote...[color=blue]
      > I'm trying to write a class for a smart pointer. I'm having a problem
      > though when trying to implement operator== with both sides being a Smart.
      > (Or any function that I try to bring U into.)
      > The pointer contained might not be the exact same type but still get
      > tested for equality. Here's what I have:
      >
      >
      > template <typename T> class Smart;
      >
      > template<typena me T, typename U>
      > bool operator== ( const Smart<T>& a, const Smart<U>& b );
      >
      > template <typename T> class Smart
      > {
      > /* On the next line, GCC says:
      > "type/value mismatch at argument 1 in template parameter list for
      > `template<class T> class Smart expected a type, got `U'"
      > */
      >
      > friend bool operator== ( const Smart<T>& a, const Smart<U>& b );[/color]

      You have to say that this is from a template:

      template<class T, class U> friend bool operator ==(...

      otherwise, what's "U"?

      And unfortunately, it's impossible to make a partial specialisation
      a friend.
      [color=blue]
      > // friend bool operator==<> ( const Smart<T>& a, const Smart<U>& b );
      >
      > }[/color]
      ^^^
      You forgot the semicolon here...
      [color=blue]
      >
      > bool operator== (const Smart<T>& a, const<SmartU>& b)[/color]

      Again, this is not right. What's "T" and "U"? What's const<SmartU>?
      Why don't you post the real code instead of typing it here?
      [color=blue]
      > {
      > return true;
      > }
      >
      >
      > It also warns about the angle brackets not being there, which are in the
      > commented out line, but adding them does not solve the error.
      >
      > What must I do to make the compiler happy?[/color]

      You probably want to find a good book on templates.

      Here is what compiles:
      ----------------------------------------
      template<class T> class Smart;

      template<class T,class U>
      bool operator== ( const Smart<T>&, const Smart<U>&);

      template<class T> class Smart
      {
      template<class S, class U>
      friend bool operator==( const Smart<S>&, const Smart<U>&);
      };

      template<class T, class U>
      bool operator== (const Smart<T>& a, const Smart<U>& b)
      {
      return true;
      }

      int main()
      {
      Smart<int> si;
      Smart<bool> sb;
      si == sb;
      }

      ----------------------------------------

      HTH

      Victor


      Comment

      • Bonzo

        #4
        Re: friend template function with multiple types

        In article <vgrk4vaiivdr1c @corp.supernews .com>,
        "Victor Bazarov" <v.Abazarov@att Abi.com> wrote:
        [color=blue]
        > You forgot the semicolon here...
        >[color=green]
        > >
        > > bool operator== (const Smart<T>& a, const<SmartU>& b)[/color]
        >
        > Again, this is not right. What's "T" and "U"? What's const<SmartU>?
        > Why don't you post the real code instead of typing it here?[/color]

        Good question. I made a stripped down version just to demonstrate the
        error. I actually *did* compile it, getting the same error, was
        satisfied, and posted it. Unfortunately, there were plenty of new errors
        after the one I posted about.

        After spending the better part of the day upping my template knowledge,
        stripping out all the friend stuff for member functions, and making
        changes similar to your code, not only does it work but I understand it.
        Many thanks.

        -Jim

        Comment

        • Bonzo

          #5
          Re: friend template function with multiple types

          In article <bekjln$606us$1 @ID-196037.news.uni-berlin.de>,
          "John Harrison" <john_andronicu s@hotmail.com> wrote:
          [color=blue]
          > First of I'd question the need to compare pointers of different type, normal
          > pointers don't work like that so why should smart pointers?[/color]

          Yeah, I ripped that code out. I wouldn't be using it. It was in some
          smart pointer code I was basing mine off of.
          [color=blue]
          > Secondly I'd question the need for friendship at all. Most smart pointers
          > have a member function that lets you get at the raw pointer[/color]

          As did I, but I didn't really understand what was going on enough. Now
          that I do, I kicked my friends out and added some member functions
          instead.

          Thank you everyone for the replies.

          -Jim

          Comment

          Working...