ctor ambiguity

S

subramanian100in

I have tried the following in VC++ 2005 Express Edition and g++ in
Linux.

Consider the class

class my_complex
{
public:
my_complex(double r, double i = 10.0) : re(r), im(i) { }
my_complex(double r) : re(r), im(0) { }
private:
double re;
double im;
};

If I create an object like
my_complex c(100.0);
the compiler generates error as expected, due to ambiguity in the
ctors.

However if I do not create an object of my_complex, the compiler does
not
generate any warning or error. Why doesn't the compiler detect the
ambiguity
when an object of type my_complex is not created ? kindly explain.
 
A

Alf P. Steinbach

* (e-mail address removed), India:
I have tried the following in VC++ 2005 Express Edition and g++ in
Linux.

Consider the class

class my_complex
{
public:
my_complex(double r, double i = 10.0) : re(r), im(i) { }
my_complex(double r) : re(r), im(0) { }
private:
double re;
double im;
};

If I create an object like
my_complex c(100.0);
the compiler generates error as expected, due to ambiguity in the
ctors.

Decide whether you want the default for im to be 0.0 or 10.0.

However if I do not create an object of my_complex, the compiler does
not
generate any warning or error. Why doesn't the compiler detect the
ambiguity
when an object of type my_complex is not created ?

There's no ambiguity if you're not trying to call one of those constructors.

The ambiguity is not caused by creating an object: you can define some
other constructor and create an object using that constructor; e.g., you
should be able to do

my_complex heh = heh;

using the automatically generated copy constructor (which is an
interesting case of circumventing the C++ initialization guarantee).
 
Z

Zeppe

However if I do not create an object of my_complex, the compiler does
not
generate any warning or error. Why doesn't the compiler detect the
ambiguity
when an object of type my_complex is not created ? kindly explain.
Oh, well, strictly speaking there is no ambiguity as soon as you ask for
a function that has multiple candidates. I mean, if you create an object

my_complex c(1.0,1.0);

it should be fine. You should wonder why the compiler shouldn't warn you
about the fact that you can't solve any more the function call if you
give just one argument.

There is indeed a general way to solve the ambiguity: consider this
simpler code:

int f()
{
return 0;
}

int f(int i = 0)
{
return i;
}

int main()
{
int (*fp)(void) = f;
fp();
return 0;
}


Bye!

Zeppe
 
R

Ron Natalie

I have tried the following in VC++ 2005 Express Edition and g++ in
Linux.

Consider the class

class my_complex
{
public:
my_complex(double r, double i = 10.0) : re(r), im(i) { }
my_complex(double r) : re(r), im(0) { }
private:
double re;
double im;
};

If I create an object like
my_complex c(100.0);
the compiler generates error as expected, due to ambiguity in the
ctors.

However if I do not create an object of my_complex, the compiler does
not
generate any warning or error. Why doesn't the compiler detect the
ambiguity
when an object of type my_complex is not created ? kindly explain.

There's no ambiguity in the case of:
my_complex(5.0, 5.0)
for example.

The compiler might notice that the one arg'd constructor can't be
unambiguously reached, but it's not required, and frankly if your
program never creates an object like my_complex(100.0), it would
not be an issue.
 

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

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top