Boris wrote in news:41******** *************** **@posting.goog le.com in
comp.lang.c++:
I have written a program that compiles just fine with GCC 3.3. With
GCC 3.4.1 it results in this error:
g++ -O0 -g3 -Wall -c -oTestp.o ../Testp.c
../Testp.c: In function `int main()':
../Testp.c:26: error: no match for 'operator<=' in 't <= 2.0e+0'
Please help me, I have no idea where the problem is.
The problem is that double isn't an exact match for a deducable
paramiter:
template <class T> class BasisF;
/* Prevent the compiler from triing to deduce T from the second
argument, and force the conversion.
*/
template < typename T > struct identity { typedef T type; };
template <class T>
inline bool operator <= (
BasisF<T> const &a,
typename identity< BasisF<T> >::type const &b
)
{
return a.x <= b.x;
}
template<class T>
class BasisF
{
public:
T x;
BasisF( T const &_x = T() ) : x( _x) {}
BasisF(BasisF const &_b) : x( _b.x ) {}
~BasisF() {}
BasisF& operator= (T const &_x)
{
x = _x;
return *this;
}
};
#define min(a,b) ((a) <= (b) ? (a) : (b))
int main()
{
BasisF<double> t(1);
/* Note only 0, EXIT_SUCCESS and EXIT_FAILURE (both from <cstdlib>) are
standard (portable) return values for main().
*/
if (min(t,2.0)<=0) return 1;
return 0;
}
The problem with the above is 0.0 <= BaseF< double >( 0.0 ) doesn't work.
Here's a version that does:
template <class T> class BasisF;
/* Boilerplate
*/
template < typename Left, typename Right >
struct BasisF_comp_ena ble
{
};
template < typename T >
struct BasisF_comp_ena ble< BasisF< T >, BasisF< T > >
{
/* enable_if return type
*/
typedef bool bool_type;
/* actual type to compare
*/
typedef BasisF< T > comp_type;
};
/* Left handed version
*/
template < typename T >
struct BasisF_comp_ena ble< BasisF< T >, T >
{
typedef bool bool_type;
typedef BasisF< T > comp_type;
};
/* Rhight handed version
*/
template < typename T >
struct BasisF_comp_ena ble< T, BasisF< T > >
{
typedef bool bool_type;
typedef BasisF< T > comp_type;
};
template < typename Left, typename Right >
inline typename BasisF_comp_ena ble< Left, Right >::bool_type
operator <= ( Left const &left, Right const &right )
{
typedef typename BasisF_comp_ena ble< Left, Right >::comp_type type;
type const &lhs = left;
type const &rhs = right;
return lhs.x < rhs.x;
}
template<class T>
struct BasisF
{
T x;
BasisF( T const &_x = T() ) : x( _x) {}
};
#include <iostream>
#include <ostream>
#include <iomanip>
int main()
{
using namespace std;
BasisF<double> t(1);
cout << boolalpha << ( t <= 2.0 ) << '\n';
cout << boolalpha << ( 0.0 <= t ) << '\n';
}
There is a lot of boilerplate above, but you can presumably
reuse it for other operators, but check out the links bellow.
links:
http://boost-consulting.com/boost/li...enable_if.html
<
http://groups.google.co.uk/groups?hl=en&lr=&ie=UTF-8
&q************* *************** *************** *@metrowerks.co m>
aka:
http://tinyurl.com/5kh86
Rob.
--
http://www.victim-prime.dsl.pipex.com/