• Yedidya Feldblum's avatar
    Define to_narrow in terms of implicit conversion · f3dfa82a
    Yedidya Feldblum authored
    Summary:
    [Folly] Define `to_narrow` in terms of implicit conversion to take advantage of ubsan's truncation checks.
    
    Note that integer truncation is not undefined behavior per se but is commonly unintended, so ubsan checks for it. Uses of `to_narrow` should not be subject to truncation; wherever truncation is anticipated, better to use an explicit conversion with a named destination type.
    
    Requires suppressing `-Wconversion` around the implicit conversion to avoid extraneous warnings and errors when the compiler detects narrowing implicit conversions. Since `to_narrow` models explicit conversion while merely being defined in terms of implicit conversion, it is already anticipated by all users that a narrowing conversion may occur; the warning is there to inform users only about unanticipated cases.
    
    Reviewed By: luciang
    
    Differential Revision: D19201430
    
    fbshipit-source-id: b3739d3d9aa05e95f7b3e4dff54e719691c1f04c
    f3dfa82a
Utility.h 11.2 KB