Re: Constructor question...
On 19/02/2013 20:40, Tobias M??ller wrote:
That's not quite true. One of them has to be convertible to the type of the
other.
Since A has only one constructor, they have to be convertible to a common
type (let's call it Z) anyway. That means, with an appropriate cast on
either x or y, the ternary operator is perfectly suitable for any possible
combination of x, y and Z.
Luca Risolia is right in that the OP hasn't provided sufficient
information to give an accurate answer.
That's where we disagree.
If you define A (according to the OP's requirements), x and y as
follows, then, although the OP example compiles without problems, "A
a(cond ? x : y);" will not compile:
struct A {
A(int = 0) {};
operator int() {};
};
int main() {
int x;
A y;
A a(true ? x : y); // error: ambiguous
}
source.cpp:9:13: error: conditional expression is ambiguous; 'int' can
be converted to 'A' and vice versa
A a(true ? x : y);
Therefore, as I said, the answer to the OP question in general depends
on the definition of A and the types of x and y, which were not given in
the question.