c++ - No dangling reference for std::min in libc++ -
it known (or should be) binding result of std::min
const
reference bad idea, whenever 1 of arguments of std::min
rvalue, since const
reference binding not propagated through function return. following code
#include <iostream> #include <algorithm> int main() { int n = 42; const int& r = std::min(n - 1, n + 1); // r dangling after line std::cout << r; }
should produce undefined behaviour since r
dangling. , indeed, when compiling gcc5.2 -wall -o3
compiler spits
warning:
<anonymous>
used uninitialized in function [-wuninitialized]
however, compiling clang (llvm 7.0.0) using same flags (even including -wextra
) not emit warning, , program seems "work", i.e. displays 41
.
question: clang using "safe" version of std::min
? version uses sfinae return value whenever 1 of arguments rvalue? or not required emit diagnostic , program "happens" produce "right" result in ub scenario?
it ub. libc++ not protect in way.
Comments
Post a Comment