auto Function with if Statement won't return a value

前端 未结 3 1852
醉话见心
醉话见心 2021-01-19 18:11

I made a template and an auto function that compare 2 values and return the smallest one. This is my code:

#include 

        
3条回答
  •  醉话见心
    2021-01-19 18:42

    A function can only have a single return type. Using return type deduction means that it will be deduced based on the type of the expression in the first return statement the parser sees. If later return statements do not return expressions of the same type, then the function is considered to be self-contradictory and thus ill-formed.

    In the second case, the ?: determines the type of the expression based on a common type determined based on the second and third sub-expressions. The two sub-expressions will be converted to this common type.

    That's different from how return type deduction works. If you intend for your first case to work, then you need to explicitly convert the returned value to the desired return type.

提交回复
热议问题