It depends on how the template is used. For an int
, you're fine.
But, if t
is an IEEE754 floating point double
type with a value set to NaN
, neither t < 1
nor t >= 1
are true
and so program control reaches the end of the if
block! This causes the function to return without an explicit value; the behaviour of which is undefined.
(In a more general case, where T
overloads the <
and >=
operators in such a way as to not cover all possibilities, program control will reach the end of the if
block with no explicit return
.)
The moral of the story here is to decide on which branch should be the default, and make that one the else
case.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…