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

  • SEARCH
  • Home
  • 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 6095677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:50:20+00:00 2026-05-23T12:50:20+00:00

I am trying to create an example, which would check the existence of the

  • 0

I am trying to create an example, which would check the existence of the operator== (member or, non-member function). To check whether a class has a member operator== is easy, but how to check whether it has a non-member operator==?

This is what I have to far :

#include <iostream>

struct A
{
    int  a;

    #if 0
    bool operator==( const A& rhs ) const
    {
        return ( a==rhs.a);
    }
    #endif
};
#if 1
bool operator==( const A &l,const A &r )
{
    return ( l.a==r.a);
}
#endif


template < typename T >
struct opEqualExists
{
    struct yes{ char a[1]; };
    struct no { char a[2]; };

    template <typename C> static yes test( typeof(&C::operator==) );
    //template <typename C> static yes test( ???? );
    template <typename C> static no test(...);

    enum { value = (sizeof(test<T>(0)) == sizeof(yes)) };
};

int main()
{
    std::cout<<(int)opEqualExists<A>::value<<std::endl;
}

Is it possible to write a test function to test the existence of non-member operator==?
If yes, how?

btw I have checked similar questions, but haven’t found a proper solution :
Is it possible to use SFINAE/templates to check if an operator exists?

This is what I tried :

template <typename C> static yes test( const C*,bool(*)(const C&,constC&) = &operator== );

but the compilation fails if the non-member operator== is removed

  • 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-23T12:50:21+00:00Added an answer on May 23, 2026 at 12:50 pm

    C++03

    The following trick works and it can be used for all such operators:

    namespace CHECK
    {
      class No { bool b[2]; };
      template<typename T, typename Arg> No operator== (const T&, const Arg&);
    
      bool Check (...);
      No& Check (const No&);
    
      template <typename T, typename Arg = T>
      struct EqualExists
      {
        enum { value = (sizeof(Check(*(T*)(0) == *(Arg*)(0))) != sizeof(No)) };
      };  
    }
    

    Usage:

    CHECK::EqualExists<A>::value;
    

    The 2nd template typename Arg is useful for some special cases like A::operator==(short), where it’s not similar to class itself. In such cases the usage is:

    CHECK::EqualExists<A, short>::value
    //                    ^^^^^ argument of `operator==`
    

    Demo.


    C++11

    We need not use sizeof and null reference trick when we have decltype and std::declval

    namespace CHECK
    {
      struct No {}; 
      template<typename T, typename Arg> No operator== (const T&, const Arg&);
    
      template<typename T, typename Arg = T>
      struct EqualExists
      {
        enum { value = !std::is_same<decltype(std::declval<T>() < std::declval<Arg>()), No>::value };
      };  
    }
    

    Demo

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

Sidebar

Related Questions

In my example, I'm trying to create a new object which has references to
I am trying to create a batch script which would perform the same action
I'm trying to create a general library which would have all methods for managing
I have recently being trying to create a project which has several levels of
I'm studying webrat and cucumber and trying to create simple example. Here is my
I'm trying to create a simple example of an editable gridview, and for some
I'm trying to create a simple example to understand how to store then retrieve
I am trying to create a slider like this example. pls anyone help me.
I am trying to create dynamic menus from the database using the following example
I'm trying to create an access control system. Here's a stripped down example of

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.