This program compiles fine with g++3.4.3 as
g++ -std=c++98 -pedantic -Wall -Wextra x.cpp
and produced the output
First:perator Second() called
Second::default ctor called. val = -1000
From print(): -1000

I have the following question(for learning purpose only)
the print() function takes a 'Second' type object and I am passing a
'First' type object. From 'First' type to 'Second' type, there are two
possibilities namely,
First:perator Second(void)
and
Second::Second(const First& arg)
Am I correct ? If so, then why doesn't the compiler give ambiguity
error for the two possibilities but instead picks up First:perator
Second() ?

I am unable to understand this. Kindly clarify this. What does the
Standard say regarding which one among these two functions to be
chosen ?

The first only works if the expression to be converted is a
non-const.
> If so, then why doesn't the compiler give ambiguity error for
> the two possibilities but instead picks up First:perator
> Second() ?

For the same reasons the compiler generally prefers a non-const
function over a const--- in this case, First:perator Second()
is an exact match, and Second::Second( First const& ) requires a
const conversion. In general, given a non-const lvalue, the
compiler will prefer a function which treats the object as a
non-const to one which treats it as const.
> I am unable to understand this. Kindly clarify this. What does
> the Standard say regarding which one among these two functions
> to be chosen ?

That First:perator Second() should be preferred for non-const
values, and Second::Second( First const& ) for const values.

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!