Friend function of template inner class?

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

    Friend function of template inner class?

    The goal is to make a friend function of an inner template class...

    template < typename T >
    class Foo {
    public:
    class Bar {
    friend bool operator==(
    const typename Foo<T>::Bar& lhs, const typename Foo<T>::Bar& rhs);
    };
    };

    template < typename T >
    bool operator==(cons t typename Foo<T>::Bar& lhs, const typename
    Foo<T>::Bar& rhs)
    {
    bool result = false;
    return result;
    }

    #include <cassert>

    int main()
    {
    Foo<int>::Bar a;
    bool r = a == a;
    assert( r == false );
    }

    Comeau says:

    "bool operator==(cons t Foo<T>::Bar &, const Foo<T>::Bar &)" declares a
    non-template function -- add <to refer to a template instance
    friend bool operator==(cons t typename Foo<T>::Bar& lhs, const
    ^
    typename Foo<T>::Bar& rhs);

    GCC compiles but complains at link time:
    ZeroLink: unknown symbol '__ZeqRKN3FooIi E3BarES3_'

    What am I doing wrong?
  • Ian Collins

    #2
    Re: Friend function of template inner class?

    Daniel T. wrote:
    The goal is to make a friend function of an inner template class...
    >
    template < typename T >
    class Foo {
    public:
    class Bar {
    friend bool operator==(
    const typename Foo<T>::Bar& lhs, const typename Foo<T>::Bar& rhs);
    };
    };
    >
    template < typename T >
    bool operator==(cons t typename Foo<T>::Bar& lhs, const typename
    Foo<T>::Bar& rhs)
    {
    bool result = false;
    return result;
    }
    >
    #include <cassert>
    >
    int main()
    {
    Foo<int>::Bar a;
    bool r = a == a;
    assert( r == false );
    }
    >
    Comeau says:
    >
    "bool operator==(cons t Foo<T>::Bar &, const Foo<T>::Bar &)" declares a
    non-template function --
    That's correct, the friend operator is not a member function, it is a
    normal function with const Foo<T>::Bar& are parameters.

    In these cases, it's much clearer to inline the operator. If the
    operator is not trivial, have it call member functions.

    --
    Ian Collins.

    Comment

    • Barry

      #3
      Re: Friend function of template inner class?

      On 9ÔÂ18ÈÕ, ÏÂÎç12ʱ10·Ö, "Daniel T." <danie...@earth link.netwrote:
      The goal is to make a friend function of an inner template class...
      >
      template < typename T >
      class Foo {
      public:
      class Bar {
      friend bool operator==(
      const typename Foo<T>::Bar& lhs, const typename Foo<T>::Bar& rhs);
      };
      >
      };
      >
      template < typename T >
      bool operator==(cons t typename Foo<T>::Bar& lhs, const typename
      Foo<T>::Bar& rhs)
      {
      bool result = false;
      return result;
      >
      }
      >
      #include <cassert>
      >
      int main()
      {
      Foo<int>::Bar a;
      bool r = a == a;
      assert( r == false );
      >
      }
      >
      Comeau says:
      >
      "bool operator==(cons t Foo<T>::Bar &, const Foo<T>::Bar &)" declares a
      non-template function -- add <to refer to a template instance
      friend bool operator==(cons t typename Foo<T>::Bar& lhs, const
      ^
      typename Foo<T>::Bar& rhs);

      Actually, "Comeau Online" does NOT link. so it doesn't produce linkage
      error
      >
      GCC compiles but complains at link time:
      ZeroLink: unknown symbol '__ZeqRKN3FooIi E3BarES3_'
      >
      What am I doing wrong?
      I. The easiest way to do this is define it inside of Foo::Bar;

      ....
      ....
      friend operator== (Bar const& lhs, Bar const& rhs) { ... }
      ....
      ....

      II. define it out side of Foo::Bar;

      according to 14.5.3/1

      your friend declaration of "operator== " is not a template function,
      unless you add "<T>" after "operator== ", meanwhile if you do this,
      you have to forward declaring this templated "operator== ", which also
      needs you to forward declaring template class "Foo".

      But in this case, you also get into the trouble of deducing T from
      "bool r = a == a;" with a==Foo<int>::Ba r, so IMHO, you can't do it
      in this way.

      Besides, according how you declared friend "operator== " inside
      Foo::Bar,
      which was a non-template operator, you can specialize every T for
      "operator== "
      out side of Foo::Bar, like this:

      bool operator== (Foo<int>::Bar const& lhs,
      Foo<int>::Bar const& rhs)
      {
      ...
      }


      --
      Best Regards
      Barry

      Comment

      • PeterAPIIT@gmail.com

        #4
        Re: Friend function of template inner class?

        Hello Barry, Where you get the C++ standard reference ?

        Thanks for your help.

        Comment

        • Ian Collins

          #5
          Re: Friend function of template inner class?

          PeterAPIIT@gmai l.com wrote:

          Which reference? Keep the context you are replying to.
          Hello Barry, Where you get the C++ standard reference ?
          >
          My guess would be from his copy of the standard...

          --
          Ian Collins.

          Comment

          • PeterAPIIT@gmail.com

            #6
            Re: Friend function of template inner class?

            >
            according to 14.5.3/1

            your friend declaration of "operator== " is not a template function,
            unless you add "<T>" after "operator== ", meanwhile if you do this,
            you have to forward declaring this templated "operator== ", which also
            needs you to forward declaring template class "Foo".
            I looking for this reference. I guess this is a C++ standard
            specification.

            A billion thanks for your help.

            Comment

            Working...