3 ms·
> For example, if a function has multiple return paths, they need to have the same type. > ... > [Code sample that returns -1 or record->id] > Instead of usi
by freditup 12y ago
> For example, if a function has multiple return paths, they need to have the same type.
> ...
> [Code sample that returns -1 or record->id]
> Instead of using an integral type to identify the person in the record object, maybe I have a new GUID type. Making that change to the record object will cause a series of cascading changes in things like the return types of functions. But if my function uses automatic return type deduction, the compiler will silently make the change for me.
Yet when this happens, his example function can now return -1 or a GUID, so compilation should fail, no? Haven't worked with C++ in awhile so I might just be missing something on this, but seems like a contradiction to me.
- ubercore 12y agoNo, the return type can change as long as all return paths return the same value for the same function call. So you can't, in the same execution, return two different types, but two different executions can vary their return type as long as its consistent.
- deleted 12y ago[deleted]
- pyrois 12y agoI'm not sure if implicit conversions are applied to type deductions. It could be that GUID has a constructor that takes in an integer, which would mean that if the function were declared with a return type of "GUID", returning -1 would be fine. I don't know if the same applies to functions with "auto" as the return type, but the example with the int and the double suggests not. So, this is a long-winded way of saying "yes, I think it's a bug".