should it be ambigious?

  • Thread starter =?ISO-8859-1?Q?Daniel_Sch=FCle?=
  • Start date
?

=?ISO-8859-1?Q?Daniel_Sch=FCle?=

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::eek:perator double()?

Thx in advance

-Daniel
 
V

Victor Bazarov

Daniel said:
#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::eek:perator double()?

No, why should it? If you wrote

1 + double(t)

then it would.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,661
Latest member
sxarexu

Latest Threads

Top