Hello, is there a way I can implement the ability to go (a==b==c) on instances instead of (a==b && b==c)
Thanks
Printable View
Hello, is there a way I can implement the ability to go (a==b==c) on instances instead of (a==b && b==c)
Thanks
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.Quote:
Originally Posted by Tux0r
It's a bad habit from writing too much C code. :devil:
>> 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;
}
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.Quote:
Originally Posted by Tux0r
Obfuscation at its best.
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.Quote:
Originally Posted by Tux0r
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?
Yeah I tried count but it looks like std::adjacent_find is the best solution.
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';
}
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.Quote:
Originally Posted by adeyblue
Probably the cleanest way would be to define your own function:
You can implement it using STL algorithms.Code:template <class Iter, class Pred>
bool all(Iter from, Iter to, Pred pred);
Obviously, but I'm not the one who likes to reinvent the wheel.