g++ -Wuninitialized doesn't show a warning at std::min()

Viewed 69

I compiled the following code with g++ -Wuninitialized (g++ Version 7.5.0) and I didn't get any warning:

#include <algorithm>

int main() {

    int a, b;

    b = std::min(a, 0);

}

As you can see, std::min() is called with the parameters a and 0, but a isn't initialized at this point. This should give you a warning, but it doesn't.

If you replace std::min() with a custom function, the compiler will give you a warning:

Code

int f(int x) {
  
    return 2 * x;

}

int main() {

    int a, b;

    b = f(a);

}

Warning

<source>: In function 'int main()':
<source>:11:10: warning: 'a' is used uninitialized in this function [-Wuninitialized]
   11 |     b = f(a);
      |         ~^~~

Why does this not work with std::min()? I also tried std::max() and it also didn't give me a warning. But with std::hypot() I get one. Is there a way to make it also work for std::min()?

0 Answers
Related