X
xtrigger303
Hi to all,
I was reading Mr. Alexandrescu's mojo article and I've a hard time
understanding the following.
Let's suppose I have:
//code
struct A {};
struct B : A {};
struct C
{
operator A() const { return A(); }
operator B() { return B(); }
};
void F( A const & ) {}
void F( B const & ) {}
C const H() { return C(); }
int main()
{
F( C() ); // 1. called with a non-const rvalue
F( H() ); // 2. called with a const rvalue
}
//end code
I understand that if I have a const "C" (rvalue or lvalue, here it
does not matter)
the only option when calling "F" would be to use
C:perator A() const -> F( A const & )
In the case I have a non-const "C" it seems that the compiler
chooses
C:perator B() -> F( B const & )
my understanding of the article is that this is because since
B is derived from A, it's a better match than the base class.
I do not understand then why the constness of operator A() makes
the difference. If I remove it and make it just
C:perator A()
my guess would be that that when calling "F" I would always get a call
to
F( B const & ). But instead the thing does not compile.
My naive understanding then is that the constness of the conversion
operators
AND the fact the B is derived from A all come into play when
"resolving" the call
to "F". The problem is that I cannot fully understand the logic behind
this nor
I can find the right chapter in the standard. Tryed [over.ics.rank].
Any help to understand this better would be greatly appreciated.
Thanks in advance,
Francesco
I was reading Mr. Alexandrescu's mojo article and I've a hard time
understanding the following.
Let's suppose I have:
//code
struct A {};
struct B : A {};
struct C
{
operator A() const { return A(); }
operator B() { return B(); }
};
void F( A const & ) {}
void F( B const & ) {}
C const H() { return C(); }
int main()
{
F( C() ); // 1. called with a non-const rvalue
F( H() ); // 2. called with a const rvalue
}
//end code
I understand that if I have a const "C" (rvalue or lvalue, here it
does not matter)
the only option when calling "F" would be to use
C:perator A() const -> F( A const & )
In the case I have a non-const "C" it seems that the compiler
chooses
C:perator B() -> F( B const & )
my understanding of the article is that this is because since
B is derived from A, it's a better match than the base class.
I do not understand then why the constness of operator A() makes
the difference. If I remove it and make it just
C:perator A()
my guess would be that that when calling "F" I would always get a call
to
F( B const & ). But instead the thing does not compile.
My naive understanding then is that the constness of the conversion
operators
AND the fact the B is derived from A all come into play when
"resolving" the call
to "F". The problem is that I cannot fully understand the logic behind
this nor
I can find the right chapter in the standard. Tryed [over.ics.rank].
Any help to understand this better would be greatly appreciated.
Thanks in advance,
Francesco