automatic type conversion

R

Rahul

Hi Everyone,

I have the following code,


class B;

class A
{
public : operator B();
};

class B
{
public : B()
{
}
B(const A& ref)
{
printf("destination conversion\n");
}

B(const B& ref)
{
printf("copy constructor\n");
}
};

A::eek:perator B()
{
printf("source conversion\n");
return B();
}
int main()
{
A obj;
B obj1 = obj;
return(0);
}

so the output is

destination conversion.

There are actually two ways to convert from obj to obj1
1) destination conversion, constructor function of B
2) source conversion, operator function of A and then copy constructor
of B

The compiler has selected the first option, where as i expected a
ambiguity compile time error, what does the standard say for this?
 
V

Victor Bazarov

Rahul said:
Hi Everyone,

I have the following code,


class B;

class A
{
public : operator B();
};

class B
{
public : B()
{
}
B(const A& ref)
{
printf("destination conversion\n");
}

B(const B& ref)
{
printf("copy constructor\n");
}
};

A::eek:perator B()
{
printf("source conversion\n");
return B();
}
int main()
{
A obj;
B obj1 = obj;
return(0);
}

so the output is

destination conversion.

There are actually two ways to convert from obj to obj1
1) destination conversion, constructor function of B
2) source conversion, operator function of A and then copy constructor
of B

The compiler has selected the first option, where as i expected a
ambiguity compile time error, what does the standard say for this?

Your declaration/definition/initialisation statement

B obj1 = obj;

is actually another form of writing

B obj1((B(obj)));

(and the copying from the temporary of type B to 'obj1' is allowed
to be optimized away by the compiler). As you can see, the temporary
is created from 'obj'. There are two ways to create it: by means of
the converting constructor in B or by means of creating *another*
temporary from A::eek:perator B() and then copy-constructing the original
temporary. The compiler chooses the short way, I guess.

V
 
R

Rahul

Your declaration/definition/initialisation statement

B obj1 = obj;

is actually another form of writing

B obj1((B(obj)));

(and the copying from the temporary of type B to 'obj1' is allowed
to be optimized away by the compiler). As you can see, the temporary
is created from 'obj'. There are two ways to create it: by means of
the converting constructor in B or by means of creating *another*
temporary from A::eek:perator B() and then copy-constructing the original
temporary. The compiler chooses the short way, I guess.

V

But the moment i have the following code,

A obj;
B obj1;
obj1 = obj;

I get an error, and the compiler is not able to decide between the
constructor conversion and the operator conversion... So i expected a
similar error in the initial posted code... Does anyone know what the
standard says for these cases?
 
R

Rahul

Rahul said:
class A
{
public : operator B();
};
class B
{
public : B()
{
}
B(const A& ref)
{
printf("destination conversion\n");
}
[remainder redactged]

This is homework. (e-mail address removed) posted the exact same question.

To vairavans and Rahul, please see FAQ 5.2:www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.2

I don't understand what possible home work do u make out of it?
I'm just trying to understand cases where compiler spots an
ambiguity... besides the complexity just increases with operator=,
copy constructor and their combination with source converter or
destination converter or both...
Naturally one would like to clear out the basic simple cases
first...
 
R

red floyd

Rahul said:
Rahul said:
class B;
class A
{
public : operator B();
};
class B
{
public : B()
{
}
B(const A& ref)
{
printf("destination conversion\n");
}
[remainder redactged]
This is homework. (e-mail address removed) posted the exact same question.

To vairavans and Rahul, please see FAQ 5.2:www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.2

I don't understand what possible home work do u make out of it?
I'm just trying to understand cases where compiler spots an
ambiguity... besides the complexity just increases with operator=,
copy constructor and their combination with source converter or
destination converter or both...
Naturally one would like to clear out the basic simple cases
first...


If it's not homework, then why did you and vairavans post identical code
(down to formatting!) with the same question?
 
J

James Kanze

I have the following code,
class A
{
public : operator B();
};
class B
{
public : B()
{
}
B(const A& ref)
{
printf("destination conversion\n");
}
B(const B& ref)
{
printf("copy constructor\n");
}
};
A::eek:perator B()
{
printf("source conversion\n");
return B();}
int main()
{
A obj;
B obj1 = obj;
return(0);
}
so the output is
destination conversion.
There are actually two ways to convert from obj to obj1
1) destination conversion, constructor function of B
2) source conversion, operator function of A and then copy constructor
of B
The compiler has selected the first option, where as I
expected a ambiguity compile time error, what does the
standard say for this?

That you should see "source conversion" (which is also what I
get from g++ and Sun CC). The requirement in "B obj1 = obj;" is
that obj must be converted into a B (which is then used as an
argument for the copy constructor). A::eek:perator B() is an exact
match, in every detail. B( A const& ) is classified as an exact
match, but does require adding a const, which in this case, the
compiler shoul use as a tie-breaker.

Change the code to:
A const obj ;
B obj1 = obj ;
and you should see "destination conversion", since A::eek:perator
B() can no longer be called on obj. (You'll also have to
provide a user defined default constructor for A.)

Declare A::eek:perator B() const (which it probablyl should be),
and the conversions will be ambiguous (but g++ allows it, using
"destination conversions"---an error in g++).

In practice, of course, it shouldn't matter---you should never
provide round-trip implicit conversions anyway.
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top