Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1009961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T09:00:32+00:00 2026-05-16T09:00:32+00:00

I am trying to write a class template that provides a comparison operator between

  • 0

I am trying to write a class template that provides a comparison operator between two instatiations with different template types. As is often the case, this operator is a non-member friend. A simplified example of what I am trying to achieve can be seen below.

template<typename T>
class Wrapper {
  // Base type, which must have a val() method to allow comparison
  T t_;
public:
  Wrapper(const T& t) : t_(t) {}

  // Comparison operator
  template<typename B>
  friend
  bool
  operator==(const Wrapper &a, const Wrapper<B>&b) {
    return a.t_.val()==b.t_.val();
  }
};

// First example type for Wrapper
class X {
  int x_;    
public:
  X(int x) : x_(x) {}

  int
  val() const {return x_;}
};

// Second example type for Wrapper
class Y {
  int y_;
public:
  Y(int y) : y_(y) {}

  int
  val() const {return 2*y_;}
};

int
main() {
  Wrapper<X> WX(X(4));
  Wrapper<Y> WY(Y(2));
  return WX==WY ? 1 : 0;
}

This example (g++ 4.4.0) doesn’t compile: instead it complains that y_ from Wrapper<Y> is private and so inaccessible to the friend function, and I can see why. But how can I fix this? Adding friendship to the reversed function

  template<typename B>
  friend bool operator==(const Wrapper<B> &, const Wrapper &);

into the Wrapper class template body merely causes ambiguity to the compiler. I don’t want to allow different instatiations of the Wrapper class to have access to the private members of each other – I want to restrict access to this one operator. Is this possible?

My laptop’s in peril from being defenestrated, so any ideas would be appeciated by both me and the laptop (and the window for that matter).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T09:00:33+00:00Added an answer on May 16, 2026 at 9:00 am

    Number of things:

    • When your class template is instantiated once for X and once for Y, you have two definitions of the operator==.

    • Move the operator== out of the class declaration.

    • Note that a friend is not a member. Hence the access violation related diagnostic.

    Try this:

    template<typename T>
    class Wrapper {
      // Base type, which must have a val() method to allow comparison
      T t_;
    public:
      Wrapper(const T& t) : t_(t) {}
    
      // Comparison operator
    
      template<typename A, typename B>
      friend bool
      operator==(const Wrapper<A> &a, const Wrapper<B>&b);
    };
    
    template<typename A, typename B>
      bool
      operator==(const Wrapper<A> &a, const Wrapper<B>&b) {
        return a.t_.val()==b.t_.val();
      }
    
    // First example type for Wrapper
    class X {
      int x_;    
    public:
      X(int x) : x_(x) {}
    
      int
      val() const {return x_;}
    };
    
    // Second example type for Wrapper
    class Y {
      int y_;
    public:
      Y(int y) : y_(y) {}
    
      int
      val() const {return 2*y_;}
    };
    
    int
    main() {
      Wrapper<X> WX(X(4));
      Wrapper<Y> WY(Y(2));
      return ::operator==(WX, WY) ? 1 : 0;
    }
    

    Though I still don’t like the possibility that there are two possible friend operator== lurking in there…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 520k
  • Answers 520k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer $(".gradeA, .gradeU").find(":checkbox").click(function() { if (this.checked === false) { return; }… May 16, 2026 at 8:46 pm
  • Editorial Team
    Editorial Team added an answer Impossible. Due to obvious security reasons. May 16, 2026 at 8:46 pm
  • Editorial Team
    Editorial Team added an answer I do know that the functions cout , cin etc...… May 16, 2026 at 8:46 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

noob here still experimenting with templates. Trying to write a message processing class template
I am trying to write a generic one-size-fits-most repository pattern template class for an
I am trying to implement a template class that would be able to tell
I'm trying to write a route that captures the one-to-many relationship between posts and
I'm trying to write a helper class that represents fields on an object. The
I'm trying to write a class which contains several std::vectors as data members, and
I'm trying to write a code that allows a user to load an assembly
I am trying to set up a package template in SSIS, following the Wrox
I have the following template class: template<class T> class C { typedef C_traits<T> traits;
I'm trying to write a linked queue in C++, but I'm failing so far.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.