473,395 Members | 1,452 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

should it be ambigious?

hello all,

#include <iostream>
#include <cstdlib>

using namespace std;

struct test
{
operator signed int(){ cout << "SINT" << endl; return -1; }
operator unsigned int(){ cout << "UINT" << endl; return 1; }
operator float(){ cout << "F" << endl; return 0.0; }
operator double(){ cout << "D" << endl; return 0.0; }
};

template <typename stype, typename utype>
void int_test()
{
test t;
stype s;
utype u;
t + s, t + u;
}

template <typename type>
void float_test()
{
test t;
type x;
t + x;
}

int main()
{
int_test<signed int, unsigned int>(); // 1
int_test<signed char, unsigned char>(); // 2

float_test<float>(); // 3
float_test<double>(); // 4

system("PAUSE");
return 0;
}

1, 2, 3, 4 all of them are ambigious calls
double(1) + t
shouldn't this invoke test::operator double()?

Thx in advance

-Daniel
Jul 23 '05 #1
1 1236
Daniel Schüle wrote:
#include <iostream>
#include <cstdlib>

using namespace std;

struct test
{
operator signed int(){ cout << "SINT" << endl; return -1; }
operator unsigned int(){ cout << "UINT" << endl; return 1; }
operator float(){ cout << "F" << endl; return 0.0; }
operator double(){ cout << "D" << endl; return 0.0; }
};

template <typename stype, typename utype>
void int_test()
{
test t;
stype s;
utype u;
t + s, t + u;
In this expression both 't's have to be converted. All conversions
(user-defined) are equally fitting. That's why compiler cannot pick
any particular one. The fact that 't' is added to a value of the
particular type does not have any weight in the decision.
}

template <typename type>
void float_test()
{
test t;
type x;
t + x;
}

int main()
{
int_test<signed int, unsigned int>(); // 1
int_test<signed char, unsigned char>(); // 2

float_test<float>(); // 3
float_test<double>(); // 4

system("PAUSE");
return 0;
}

1, 2, 3, 4 all of them are ambigious calls
Yes, they are. Essentially, the compiler just can't choose between
all four existing type conversion operators.
double(1) + t
What's that? You don't have this in your code.
shouldn't this invoke test::operator double()?


No, why should it? If you wrote

1 + double(t)

then it would.

V
Jul 23 '05 #2

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

Similar topics

303
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b....
3
by: Andy Visniewski | last post by:
Should be easy, but I've been trying to figure this out for about half an hour with no luck. There is a table 'Cybex' which has all the Cybex products we sell, and a table 'Datasheets' which...
36
by: Jack Hughes | last post by:
This argument has come up two or three times lately. We have a web standards document at our company that say "Use valid HTML 4; We don't recommend switching to DIV-based CSS-P for layout just...
12
by: Sanjay | last post by:
hi, We are currently porting our project from VB6 to VB .NET. Earlier we used to make scale transformations on objects like pictureBox , forms etc.Now Such transformations are made on the...
8
by: Chris Mayers | last post by:
I am trying to track a suspected memory leak in an application I'm writing and through repeated simplification of my project, I have found the following which is quite easily replicated should you...
5
by: davidw | last post by:
Last time, I asked a question about if a XPathDocument could be serialized. Actually what I really want to know is how should I do in this case: I have a class that will be serialized. In the...
6
by: Terry Bell | last post by:
We've had a very large A97 app running fine for the last seven years. I've just converted to SQL Server backend, which is being tested, but meanwhile the JET based version, running under terminal...
64
by: Morgan Cheng | last post by:
Hi All, I was taught that argument valuse is not supposed to be changed in function body. Say, below code is not good. void foo1(int x) { x ++; printf("x+1 = %d\n", x); } It should be...
8
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - When should I use eval? ----------------------------------------------------------------------- The ` eval() `...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.