I've read that the compiler is ready to take up to 2 steps of conversion , by the following order :
1.exact match
2.premotion
3.conversion
4.user defined conversion
can you please give me an example where the compiler can't take 3 steps of conversion ?
struct Foo{ Foo( int ) {} };
struct Bar{ Bar( Foo ) {} };
struct Baz{ Baz( Bar ) {} };
void bar( Bar b ) {}
int main() {
Foo a = 5;// int -> Foo is OK
Foo b = 0.5;// float -> int -> Foo is OK
Bar c = 5;// int -> Foo -> Bar is OK
bar( 5 );//int -> Foo -> Bar fails
Baz d = 5;//int -> Foo -> Bar -> Baz fails
}
Please pay attention that the line " Bar c = 5;// int -> Foo -> Bar " doesn't compile also , which is pretty weird since the conversions are
1. int to Foo
2. Foo to Bar
#include <iostream>
usingnamespace std;
class Foo
{
public:
Foo(int) {}
};
class Bar
{
public:
Bar( Foo ) {}
};
class Baz
{
public:
Baz( Bar ) {}
};
void bar( Bar b ) {}
int main() {
Foo a = 5;// int -> Foo is OK
Foo b = 0.5;// float -> int -> Foo is OK
Bar c = 5;// int -> Foo -> Bar
bar( 5 );//int -> Foo -> Bar fails
Baz d = 5;//int -> Foo -> Bar -> Baz fails
return 0;
}
That line compiled for me.. Even though the next one, which did the same thing, did not. I suppose we're just using different compilers. I failed to find anything about this in the standard.