Assuming there is an enum like this:
enum foo: int {
first,
second
}
Then I use it as follows:
foo f(1); // error: cannot initialize a variable of type 'foo' with an rvalue of type 'int'
foo f = foo(1); // OK !
I was wondering what is the difference between the two ? I understand that the second version can be seen as a functional-style cast but why does this make any difference ?
For example, if I do this:
class Bar {};
Bar b = Bar(1); // no matching conversion for functional-style cast from 'int' to 'Bar'
I obviously get an error which makes sense. Therefore, this leads me to believe that in order for the second version of the foo example above to work there must be a conversion from int
to enum
defined somewhere but if there is such a conversion then why do I get an error in the first version ?
I do apologize if this is a duplicate. I am suspecting it is. This seems relevant: Is this a cast or a construction? ... but not quit.
Thanks in advance !