C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-08-2009, 05:27 AM   #1
C++0x User
 
Tux0r's Avatar
 
Join Date: Nov 2008
Location: Sweden
Posts: 133
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
Tux0r is offline   Reply With Quote
Old 07-08-2009, 05:37 AM   #2
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,352
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
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 07-08-2009, 05:45 AM   #3
C++0x User
 
Tux0r's Avatar
 
Join Date: Nov 2008
Location: Sweden
Posts: 133
It's a bad habit from writing too much C code.
Tux0r is offline   Reply With Quote
Old 07-08-2009, 06:38 AM   #4
Guest
 
Sebastiani's Avatar
 
Join Date: Aug 2001
Posts: 4,923
>> 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;
}
Sebastiani is offline   Reply With Quote
Old 07-08-2009, 07:22 AM   #5
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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.
tabstop is offline   Reply With Quote
Old 07-08-2009, 07:23 AM   #6
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,352
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
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 07-08-2009, 07:24 AM   #7
C++0x User
 
Tux0r's Avatar
 
Join Date: Nov 2008
Location: Sweden
Posts: 133
Obfuscation at its best.
Tux0r is offline   Reply With Quote
Old 07-08-2009, 10:22 AM   #8
C++0x User
 
Tux0r's Avatar
 
Join Date: Nov 2008
Location: Sweden
Posts: 133
By the way is there a STL algo for checking if all elemens in an array are equal? I didn't find one.
Tux0r is offline   Reply With Quote
Old 07-08-2009, 10:30 AM   #9
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,352
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
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 07-08-2009, 10:39 AM   #10
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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.
tabstop is offline   Reply With Quote
Old 07-08-2009, 10:54 AM   #11
C++0x User
 
Tux0r's Avatar
 
Join Date: Nov 2008
Location: Sweden
Posts: 133
Yeah I tried count but it looks like std::adjacent_find is the best solution.
Tux0r is offline   Reply With Quote
Old 07-08-2009, 11:11 AM   #12
Hat seller extraordinaire
 
Join Date: Apr 2008
Posts: 159
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';
}
adeyblue is offline   Reply With Quote
Old 07-08-2009, 11:23 AM   #13
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,352
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
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 07-08-2009, 11:51 AM   #14
The larch
 
Join Date: May 2006
Posts: 3,082
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.

Quote:
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).
anon is offline   Reply With Quote
Old 07-08-2009, 01:47 PM   #15
C++0x User
 
Tux0r's Avatar
 
Join Date: Nov 2008
Location: Sweden
Posts: 133
Obviously, but I'm not the one who likes to reinvent the wheel.
Tux0r is offline   Reply With Quote
Reply

Tags
class, operator==

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 07:01 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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