Reputation: 2477
The page at http://en.cppreference.com/w/cpp/language/implicit_cast states that up to three conversion sequences can be done in an implicit conversion sequence:
Implicit conversion sequence consists of the following, in this order:
- zero or one standard conversion sequence
- zero or one user-defined conversion
- zero or one standard conversion sequence
What is an example of all three occurring?
Upvotes: 1
Views: 644
Reputation: 701
I don't see how all three could occur in a row. This would mean that you enable an implicit conversion between two language-defined types (let's say int*
and char
) with the intermediate of an user-defined class (call it Stuff
). In my opinion, there is no reason for which you would want to do int*
>Stuff
>char
(for example).
In other words, an implicit conversion from A
to B
means "A
can be understood as being B
". It sounds weird to add such a rule for language-defined type, and even weirder to explain it by "A
can be understood as my class C
" and "my class C
can be understood as being B
".
But for the rule to make sense, it is enough to provide an example of 1. and 2., and an example of 2. and 3. This is easy to do.
If you haven't figured it out, just consider a class (let's say Date
) that can be converted to an int
(user defined Date::int()
) and that can be created from an int
(copy constructor). Then, by considering the conversion int
>long
and short
to int
you have your examples.
Date d;
short a = 1234;
d = a; // Convertion short>int and int>Date (1. and 2.)
long b = d; // Convertion Date>int and int>long (2. and 3.)
Upvotes: 2