Equality is tested for by applying operator == () the to successive element's from each tuple in turn, the operation terminates [1] as soon as two elements are unequal.
The order of testing is from item 0 (tuple.at< 0 >()) to item tuple.size() - 1.
ntuple< int, int, int > a( 1, 2, 3 ), b( 2, 0, 0 ), c( a ); CHECK( a != b ); CHECK( a == c ); std::cout << std::boolalpha << a << " (op) " << b << '\n'; std::cout << "== " << (a == b) << '\n'; std::cout << "!= " << (a != b) << std::endl;
Test Result: gcc34 Passed, msvc80 Passed, msvc71 Passed
Output
(1,2,3) (op) (2,0,0) == false != true
The operators will work on different types of tuple.
ntuple< int, int, int > a( 1, 2, 3 ); ntuple< int, short, double > b( 2, 0, 0 ), c( 1, 2, 3 ); CHECK( a != b ); CHECK( a == c ); std::cout << std::boolalpha << std::showpoint << a << " (op) " << b << '\n'; std::cout << "== " << (a == b) << '\n'; std::cout << "!= " << (a != b) << std::endl;
Test Result: gcc34 Passed, msvc80 Passed, msvc71 Passed
Output
(1,2,3) (op) (2,0,0.000000) == false != true
The operation will not compile if both tuples are different lengths (.size()).
ntuple< int, int, int > a( 1, 2, 3 ); ntuple< int, int > b( 2, 0 ); assert( a != b );
Test Result: gcc34 Passed, msvc80 Passed, msvc71 Passed
Warning
../ntuple-compare.hpp:122: : 'bool imp::ntuple_s::equal_cons_helper(const imp::ntuple_s::cons<LH,imp::ntuple_s::null_type> &,const imp::ntuple_s::cons<RH,imp::ntuple_s::null_type> &)' : could not deduce template argument for 'const imp::ntuple_s::cons<Tail,imp::ntuple_s::null_type> &' from 'const imp::ntuple_s::cons<Head,Tail>::remainder_type' with [ Head=int, Tail=imp::ntuple_s::null_type ] ../ntuple-compare.hpp:133: see declaration of 'imp::ntuple_s::equal_cons_helper' ../ntuple-compare.hpp:122: see reference to function template instantiation 'bool imp::ntuple_s::equal_cons_helper<A1,imp::ntuple_s::cons<Head,Tail>,A2,imp::ntuple_s::null_type>(const imp::ntuple_s::cons<Head,imp::ntuple_s::cons<Head,Tail>> &,const imp::ntuple_s::cons<Head,Tail> &)' being compiled with [ A1=int, Head=int, Tail=imp::ntuple_s::null_type, A2=int ] ../ntuple-compare.hpp:151: see reference to function template instantiation 'bool imp::ntuple_s::equal_cons_helper<Lv_t,Lr_t,Rv_t,Rr_t>(const imp::ntuple_s::cons<Head,Tail> &,const imp::ntuple_s::cons<Head,imp::ntuple_s::cons<Head,imp::ntuple_s::null_type>> &)' being compiled with [ Head=int, Tail=imp::ntuple_s::cons<int,imp::ntuple_s::cons<int,imp::ntuple_s::null_type>> ] ../ntuple-compare.hpp:163: see reference to function template instantiation 'imp::meta::enable_t_<__formal,Type>::type imp::ntuple_s::operator ==<L,R>(const L &,const R &)' being compiled with [ __formal=true, Type=bool, L=imp::ntuple_s::ntuple<int,int,int>, R=imp::ntuple_s::ntuple<int,int> ] ./gensrc/more-compare-id-eq-diff-lengths.cpp:26: see reference to function template instantiation 'imp::meta::enable_t_<__formal,Type>::type imp::ntuple_s::operator !=<imp::ntuple_s::ntuple<A0,A1,A2>,imp::ntuple_s::ntuple<A0,A1>>(const L &,const R &)' being compiled with [ __formal=true, Type=bool, A0=int, A1=int, A2=int, L=imp::ntuple_s::ntuple<int,int,int>, R=imp::ntuple_s::ntuple<int,int> ] ../ntuple-compare.hpp:122: : 'bool imp::ntuple_s::equal_cons_helper(const imp::ntuple_s::cons<LH,LT> &,const imp::ntuple_s::cons<RH,RT> &)' : could not deduce template argument for 'const imp::ntuple_s::cons<T3,T4> &' from 'const imp::ntuple_s::cons<Head,Tail>::remainder_type' with [ Head=int, Tail=imp::ntuple_s::null_type ] ../ntuple-compare.hpp:117: see declaration of 'imp::ntuple_s::equal_cons_helper'
The operation will not compile if any two elements can't be tested with operator == ().
#include <cassert>
#include "imp/ntuple.hpp"
using namespace imp;
struct Empty {};
int main()
{
ntuple< int, Empty > a( 1, Empty() ), b( 1, Empty() );
assert( a == b );
}
Test Result: gcc34 Passed, msvc80 Passed, msvc71 Passed
Warning
../ntuple-compare.hpp:135: : Failed to specialize function template 'meta::enable<imp::meta::bool_and<imp::ntuple_s::is_ntuple<L>,imp::ntuple_s::is_ntuple<R>>,bool>::type imp::ntuple_s::operator ==(const L &,const R &)' With the following template arguments: 'imp::ntuple_s::cons<Head,Tail>::first_type' with [ Head=Empty, Tail=imp::ntuple_s::null_type ] 'imp::ntuple_s::cons<Head,Tail>::first_type' with [ Head=Empty, Tail=imp::ntuple_s::null_type ] ../ntuple-compare.hpp:122: see reference to function template instantiation 'bool imp::ntuple_s::equal_cons_helper<A1,A1>(const imp::ntuple_s::cons<Head,Tail> &,const imp::ntuple_s::cons<Head,Tail> &)' being compiled with [ A1=Empty, Head=Empty, Tail=imp::ntuple_s::null_type ] ../ntuple-compare.hpp:151: see reference to function template instantiation 'bool imp::ntuple_s::equal_cons_helper<Lv_t,Lr_t,Rv_t,Rr_t>(const imp::ntuple_s::cons<Head,Tail> &,const imp::ntuple_s::cons<Head,Tail> &)' being compiled with [ Head=int, Tail=imp::ntuple_s::cons<Empty,imp::ntuple_s::null_type> ] ./gensrc/more-compare-id-no-eq.cpp:11: see reference to function template instantiation 'imp::meta::enable_t_<__formal,Type>::type imp::ntuple_s::operator ==<imp::ntuple_s::ntuple<A0,A1>,imp::ntuple_s::ntuple<A0,A1>>(const L &,const R &)' being compiled with [ __formal=true, Type=bool, A0=int, A1=Empty, L=imp::ntuple_s::ntuple<int,Empty>, R=imp::ntuple_s::ntuple<int,Empty> ] ../ntuple-compare.hpp:135: : binary '==' : 'const imp::ntuple_s::cons<Head,Tail>::first_type' does not define this operator or a conversion to a type acceptable to the predefined operator with [ Head=Empty, Tail=imp::ntuple_s::null_type ]
Orderd comarison of ntuple<>'s are done with an element wise (from 0 to tuple.size() - 1) application of operator's == and <.
Elements are tested with == until a pair returns false then < is applied
The other 3 operators (<=, > and >=) are synthesized from the results of the < operation:
l <= r |
! ( r < l ) |
l > r |
( r < l ) |
l >= r |
! ( l < r ) |
ntuple< int, int, int > a( 3, 2, 1 ), b( 3, 2, 0 ); CHECK( a > b && a >= b && b < a && b <= a ); CHECK( !(a < b) && !(a <= b) && !(b > a) && !(b >= a) ); std::cout << std::boolalpha << a << " op " << b; std::cout << "\n< " << ( a < b ) << "\n<= " << ( a <= b ) << "\n> " << ( a > b ) << "\n>= " << ( a >= b ) << std::endl ;
Test Result: gcc34 Passed, msvc80 Passed, msvc71 Passed
Output
(3,2,1) op (3,2,0) < false <= false > true >= true
ntuple< int, int > a( 1, 2 ); ntuple< int, int, int > b( 1, 2, 3 ); CHECK( a < b );
Test Result: gcc34 Passed, msvc80 Passed, msvc71 Passed
Warning
../ntuple-compare.hpp:22: : 'bool imp::ntuple_s::less_cons_helper(const imp::ntuple_s::cons<LH,imp::ntuple_s::null_type> &,const imp::ntuple_s::cons<RH,imp::ntuple_s::null_type> &)' : could not deduce template argument for 'const imp::ntuple_s::cons<Head,imp::ntuple_s::null_type> &' from 'const imp::ntuple_s::cons<Head,Tail>::remainder_type' with [ Head=int, Tail=imp::ntuple_s::null_type ] ../ntuple-compare.hpp:51: see declaration of 'imp::ntuple_s::less_cons_helper' ../ntuple-compare.hpp:22: see reference to function template instantiation 'bool imp::ntuple_s::less_cons_helper<A1,imp::ntuple_s::null_type,A0,imp::ntuple_s::cons<Head,Tail>>(const imp::ntuple_s::cons<Head,Tail> &,const imp::ntuple_s::cons<Head,imp::ntuple_s::cons<Head,Tail>> &)' being compiled with [ A1=int, A0=int, Head=int, Tail=imp::ntuple_s::null_type ] ../ntuple-compare.hpp:68: see reference to function template instantiation 'bool imp::ntuple_s::less_cons_helper<Lv_t,Lr_t,Rv_t,Rr_t>(const imp::ntuple_s::cons<Head,Tail> &,const imp::ntuple_s::cons<Head,imp::ntuple_s::cons<Head,Tail>> &)' being compiled with [ Head=int, Tail=imp::ntuple_s::cons<int,imp::ntuple_s::null_type> ] ./gensrc/more-compare-id-cmp-diff.cpp:26: see reference to function template instantiation 'imp::meta::enable_t_<__formal,Type>::type imp::ntuple_s::operator <<imp::ntuple_s::ntuple<A0,A1>,imp::ntuple_s::ntuple<A0,A1,A2>>(const L &,const R &)' being compiled with [ __formal=true, Type=bool, A0=int, A1=int, A2=int, L=imp::ntuple_s::ntuple<int,int>, R=imp::ntuple_s::ntuple<int,int,int> ] ../ntuple-compare.hpp:22: : 'bool imp::ntuple_s::less_cons_helper(const imp::ntuple_s::cons<LH,LT> &,const imp::ntuple_s::cons<RH,RT> &)' : could not deduce template argument for 'const imp::ntuple_s::cons<Head,Tail> &' from 'const imp::ntuple_s::cons<Head,Tail>::remainder_type' with [ Head=int, Tail=imp::ntuple_s::null_type ] ../ntuple-compare.hpp:18: see declaration of 'imp::ntuple_s::less_cons_helper'
| [1] | This "Short circuiting" is provided by the inbuilt operator &&. |