Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is this gcc and clang optimizer bug with minmax and structured binding?

This program, built with -std=c++20 flag:

#include <iostream>

using namespace std;

int main() {
  auto [n, m] = minmax(3, 4);
  cout << n << " " << m << endl;
}

produces expected result 3 4 when no optimization flags -Ox are used. With optimization flags it outputs 0 0. I tried it with multiple gcc versions with -O1, -O2 and -O3 flags.

Clang 13 works fine, but clang 10 and 11 outputs 0 4198864 with optimization level -O2 and higher. Icc works fine. What is happening here?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The code is here: https://godbolt.org/z/Wd4ex8bej

>Solution :

The overload of std::minmax taking two arguments returns a pair of references to the arguments. The lifetime of the arguments however end at the end of the full expression since they are temporaries.

Therefore the output line is reading dangling references, causing your program to have undefined behavior.

Instead you can use std::tie to receive by-value:

#include <iostream>
#include <tuple>
#include <algorithm>

int main() {
  int n, m;
  std::tie(n,m) = std::minmax(3, 4);
  std::cout << n << " " << m << std::endl;
}

Or you can use the std::initializer_list overload of std::minmax, which returns a pair of values:

#include <iostream>
#include <algorithm>

int main() {
  auto [n, m] = std::minmax({3, 4});
  std::cout << n << " " << m << std::endl;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading