operator==

This is a discussion on operator== within the C++ Programming forums, part of the General Programming Boards category; Hello, is there a way I can implement the ability to go (a==b==c) on instances instead of (a==b && b==c) ...

  1. #1
    C++11 User Tux0r's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    135

    operator==

    Hello, is there a way I can implement the ability to go (a==b==c) on instances instead of (a==b && b==c)

    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,461
    Quote Originally Posted by Tux0r
    Hello, is there a way I can implement the ability to go (a==b==c) on instances instead of (a==b && b==c)
    I doubt you can do what you actually want to do. Besides, if you did manage it, people are likely to misinterpret it, thinking it either a mistake, or that it really means what it would normally mean. Just Say No.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++11 User Tux0r's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    135
    It's a bad habit from writing too much C code.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,670
    >> Hello, is there a way I can implement the ability to go (a==b==c) on instances instead of (a==b && b==c)

    As Laserlight pointed out, this would probably be the wrong approach. The best policy is to model conventional semantics in order to avoid confusion and prevent the introduction of unexpected bugs. Having said that, it is of course possible.

    Code:
    struct foo
    {
    	foo( int value = 0 )
    	: value( value )
    	{	}
    
    	struct proxy
    	{	
    		friend proxy operator == ( proxy const& lhs, foo const& rhs )
    		{
    			return lhs.ptr && equal( *lhs.ptr, rhs ) ? lhs : 0; 
    		}
    		
    		operator bool ( void )
    		{
    			return ptr != 0;
    		}
    
    		proxy( foo const* ptr )
    		: ptr( ptr )
    		{	}
    		
    		foo const*
    			ptr;
    	};
    
    	friend proxy operator == ( foo const& lhs, foo const& rhs )
    	{
    		return proxy( equal( lhs, rhs ) ? &rhs : 0 ); 
    	}
    	
    	friend bool equal( foo const& lhs, foo const& rhs )
    	{
    		return lhs.value == rhs.value;
    	}
    	
    	int
    		value;
    };
    
    int main( void )
    {
    	cout << bool( foo( 1024 ) == foo( 1024 ) == foo( 1024 ) ) << endl;
    	cout << bool( foo( 0 ) == foo( 1024 ) == foo( 1024 ) ) << endl;
    	cout << bool( foo( 1024 ) == foo( 0 ) == foo( 1024 ) ) << endl;
    	cout << bool( foo( 1024 ) == foo( 1024 ) == foo( 0 ) ) << endl;
    	return 0;
    }



    ITSA
    Socket Library!

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Tux0r View Post
    It's a bad habit from writing too much C code.
    ? It doesn't work in C either.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,461
    Quote Originally Posted by Tux0r
    It's a bad habit from writing too much C code.
    I do not see how that is so. Its normal meaning in C is the same as in C++: you are comparing to see if the result of a==b is equal to c, not comparing to see if a, b and c are equal.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    C++11 User Tux0r's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    135
    Obfuscation at its best.

  8. #8
    C++11 User Tux0r's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    135
    By the way is there a STL algo for checking if all elemens in an array are equal? I didn't find one.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,461
    Quote Originally Posted by Tux0r
    By the way is there a STL algo for checking if all elemens in an array are equal? I didn't find one.
    I believe you can use std::adjacent_find with std::not_equal_to to find the first pair of adjacent elements that are not equal. If no such pair exists, all the elements in the array must be equal.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Or alternatively find_first_of, using not_equal_to bound to the first element.

    Edit: Hang on, not find_first_of, what am I thinking. What about count?
    Last edited by tabstop; 07-08-2009 at 10:41 AM.

  11. #11
    C++11 User Tux0r's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    135
    Yeah I tried count but it looks like std::adjacent_find is the best solution.

  12. #12
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    611
    What about equal_range?

    Code:
    #include <vector>
    #include <algorithm>
    #include <iostream>
    
    template<class Iterator>
    bool AreAllEqual(const Iterator& begin, const Iterator& end)
    {
        std::pair<Iterator, Iterator> equalRet = std::equal_range(begin, end, *begin);
        return ((equalRet.first == begin) && (equalRet.second == end));
    }
    
    template<class Container>
    bool AreAllEqual(const Container& cont)
    {
        return AreAllEqual(cont.begin(), cont.end());
    }
    
    
    int main(int argc, char ** argv)
    {
      std::vector<int> d;
      d.push_back(9);
      d.push_back(9);
      d.push_back(9);
      d.push_back(9);
      d.push_back(9);
      std::cout << "Container: " << std::boolalpha << AreAllEqual(d) << '\n';
      int* data = &d[0];
      std::cout << "Array: " << AreAllEqual(data, data + d.size()) << '\n';
    
      d.push_back(10);  
      std::cout << "Container with 10: " << AreAllEqual(d) << std::endl;
      data = &d[0];
      std::cout << "Array with 10: " << AreAllEqual(data, data + d.size()) << '\n';
    }

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,461
    Quote Originally Posted by adeyblue
    What about equal_range?
    It requires a sorted range. If the range is sorted, we only need to compare the first and last elements to determine if all the elements are equal.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Probably the cleanest way would be to define your own function:

    Code:
    template <class Iter, class Pred>
    bool all(Iter from, Iter to, Pred pred);
    You can implement it using STL algorithms.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  15. #15
    C++11 User Tux0r's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    135
    Obviously, but I'm not the one who likes to reinvent the wheel.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stl error
    By eklavya8 in forum C++ Programming
    Replies: 8
    Last Post: 06-30-2008, 12:01 PM
  2. "error: incomplete type is not allowed"
    By Fahrenheit in forum C++ Programming
    Replies: 9
    Last Post: 05-10-2005, 09:52 PM
  3. Problem with Template Function and overloaded equality operator
    By silk.odyssey in forum C++ Programming
    Replies: 7
    Last Post: 06-08-2004, 04:30 AM
  4. operator==
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 03-26-2002, 04:24 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21