473,756 Members | 3,655 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with implicit conversion

Hello,

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.

Here is the code:

template <class T> class BasisF;

template <class T> inline bool operator<=(cons t BasisF<T> &a, const
BasisF<T> &b){return a.x <= b.x;}

template<class T> class BasisF{

public:
T x;
BasisF(const T &_x = T()): x(_x){}
BasisF(const BasisF &_b): x(_b.x){}
~BasisF(){}
BasisF& operator= (const T &_x){
x = _x;
return *this;
}
friend bool operator<=<>(co nst BasisF &a, const BasisF &b);
};

#define min(a,b) ((a) <= (b) ? (a) : (b))

int main(){
BasisF<double> t(1);
if (min(t,2.0)<=0) return 1;
return 0;
}

Regards,
Boris
Jul 22 '05 #1
3 1657
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/
Jul 22 '05 #2
Boris wrote:
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.

Here is the code:

template <class T> class BasisF;

template <class T> inline bool operator<=(cons t BasisF<T> &a, const
BasisF<T> &b){return a.x <= b.x;}

template<class T> class BasisF{

public:
T x;
BasisF(const T &_x = T()): x(_x){}
BasisF(const BasisF &_b): x(_b.x){}
~BasisF(){}
BasisF& operator= (const T &_x){
x = _x;
return *this;
}
friend bool operator<=<>(co nst BasisF &a, const BasisF &b);
You don't need this friend declaration, 'x' is public.
};

#define min(a,b) ((a) <= (b) ? (a) : (b))
Ugh! If you are starting with templates, shouldn't you be getting rid of
all macros of that sort?

int main(){
BasisF<double> t(1);
if (min(t,2.0)<=0) return 1;
return 0;
}


The compiler can't figure out what 'T' should be for the ::operator<=
template (from the macro expansion) because one is 'BasesF<double> ' and
the other is 'double'. It will not look for a conversion from those
types to some kind of common type and then use it as 'T'.

To solve it you need to add two more operators,

template<class T> bool operator<=(cons t BasisF<T>& a, const T& b)
{
return a.x <= b;
}

and

template<class T> bool operator<=(cons t T& a, const BasisF<T>& b)
{
return a <= b.x;
}

and then use literals of type 'double' to make your compiler resolve
the operators correctly. Otherwise, you could do

template<class T, class U>
bool operator<=(cons t T& a, const U& b)
{
return BasisF<T>(a).x <= BasisF<U>(b).x;
}

Victor
Jul 22 '05 #3
Boris wrote:
BasisF(const T &_x = T()): x(_x){}
The automatic transformation of a double or an int into a BasisF does
not work as you think it does.
friend bool operator<=<>(co nst BasisF &a, const BasisF &b);
Do you have a particular reason to want this operator as a friend
fonction rather than a member function?
if (min(t,2.0)<=0) return 1;


You could explicit BasisF<double>( 0) instead of 0 and the same for 2.0,
if you do not want to make <= a member function or explicitly overload
<= for BasisF<double> and double.
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
37913
by: John | last post by:
Hello all, I am trying to read in lines into a buffer from a file. Normally I would do this very low-level, but I have come to the conclusion I must stop doing everything the hard way. So, I thought I would try <fstream>. The code below causes an infinite loop in the while() statement. Debugging, I see that the buffer just has a 0 in the first position, and nothing else.
2
3096
by: Russell Reagan | last post by:
In a newer version of a chess program I am writing, I have created classes that are (more or less) drop in replacements for things that used to be plain old integer or enumerated variables (colors, piece types, squares, etc.). To accomplish this, I used implicit conversions. For instance, a color used to be: typedef int Color; // and a few constants... Now a color is (paraphrased):
1
1860
by: Christophe Poucet | last post by:
Hellom I have an issue with implicit conversions. Apparently when one calls an operator on a class Y which has a conversion operator to class X which has the operator . Sadly it will not do implicit conversion for this case, however if instead of class X we just use a pointer then the implicit conversion will work. Anyone have any ideas why it will not do implicit conversion?
17
16602
by: Jon Slaughter | last post by:
I'm having a little trouble understanding what the slicing problem is. In B.S.'s C++ PL3rdEd he says "Becayse the Employee copy functions do not know anything about Managers, only the Employee part of Manager is copied. ".... and gives the code above as .....
11
7622
by: Steve Gough | last post by:
Could anyone please help me to understand what is happening here? The commented line produces an error, which is what I expected given that there is no conversion defined from type double to type Test. I expected the same error from the following line, but it compiles fine. The double is silently truncated to an int and then fed in to the implicit conversion operator. Why does this happen? Is there any way that I can keep the implicit...
9
2085
by: Girish | last post by:
Im trying to understand implicit type conversions from object -> string and vice versa. I have two classes, one Driver and one called StringWrapper. These are just test classes that try and emulate the pattern im trying to follow in an existing project. These are my steps: 1) I have a method "printMe" existing in the application which originally used to take in a string. This method is static and sits in the Driver
11
12793
by: Aaron Queenan | last post by:
Given the classes: class Class { public static implicit operator int(Class c) { return 0; } } class Holder
36
3634
by: Chad Z. Hower aka Kudzu | last post by:
I have an implicit conversion set up in an assembly from a Stream to something else. In C#, it works. In VB it does not. Does VB support implicit conversions? And if so any idea why it would work in a C# program but not VB? -- Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/ "Programming is an art form that fights back"
39
19646
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1) What's the difference between these 3 statements: (i) memcpy(&b, &KoefD, n); // this works somewhere in my code
3
4620
by: utab | last post by:
Dear all, I was trying to write a more complex program, and while searching for sth in my reference C++ primer, by Lippman. I had a question in the mind, see the code below #include <string> #include <iostream> #include <cstring> int main(){
0
9275
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9872
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9713
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8713
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5142
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3805
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2666
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.