By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 898 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

should it be ambigious?

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.