C++ (Qt)bool operator==(TVector<Size, T> const& other){ if ( std::is_class<T>::value ) { //loop through elements with T::operator== } else if ( std::is_floating_point<T>::value ) { //loop through elements with qFuzzyCompare } else if ( std::is_integral<T>::value ) { //do something else with integrals, or alternatively merge this with the first branch }}
C++ (Qt)
C++ (Qt)template<class T> bool tVectorElementCompare(T const& a, T const& b){ std::cout << "Generic comparison, using T::operator==" << std::endl; return a == b;} bool tVectorElementCompare(double const& a, double const& b){ std::cout << "Double comparison, using fuzzy compare" << std::endl; return qFuzzyCompare(a,b);} bool tVectorElementCompare(int const& a, int const& b){ std::cout << "Int comparison, using direct compare" << std::endl; return a == b;} template<int Size, class T>struct TVector { bool operator==(TVector const& other) { for (int i=0; i<Size; ++i) if ( !tVectorElementCompare(data[i], other.data[i]) ) return false; return true; } T data[Size];}; struct MyElem { bool operator==(MyElem const& other) const { return true; }}; int main(int, char**){ TVector<1, double> dvec1, dvec2; TVector<1, int> ivec1, ivec2; TVector<1, MyElem> cvec1, cvec2; std::cout << ( dvec1 == dvec2 ) << std::endl; std::cout << ( ivec1 == ivec2 ) << std::endl; std::cout << ( cvec1 == cvec2 ) << std::endl; return 0;}
C++ (Qt)#include <iostream> using namespace std; template<int Size, class T>struct TVector{ T data[Size];}; template<int Size, typename T>bool operator ==( const TVector<Size, T> &, const TVector<Size, T> & ){ cout << "operator== for T" << endl; return false;} template<int Size>bool operator ==( const TVector<Size, double> &, const TVector<Size, double> & ){ cout << "operator== for double" << endl; return false;} struct MyElem { bool operator==(MyElem const& /*other*/) const { return true; }}; int main(int, char**){ TVector<1, double> dvec1, dvec2; TVector<1, int> ivec1, ivec2; TVector<1, MyElem> cvec1, cvec2; std::cout << ( dvec1 == dvec2 ) << std::endl; std::cout << ( ivec1 == ivec2 ) << std::endl; std::cout << ( cvec1 == cvec2 ) << std::endl; return 0;}