lundi 13 avril 2020

const auto& to integer with std::min std::max

I have these codes below. What's wrong with the const auto& in this context, it caused unexpected outputs.

It works fine while compiled with gcc-4.8.5, but gets unexpected outputs with gcc-4.9.2.

If I remove the & in const auto&, it works fine with both gcc versions.

// max_dim is a protobuf filed: const auto max_dim = msg.max();
// cat2_num is an element in std::vector<int32_t>: const auto cat2_num = vec[i]
const auto& res_num = std::max(1, std::min(max_dim, cat2_num));
LOG(ERROR) << res_num << ", " << max_dim << ", " << cat2_num
   << ", " << std::max(1, std::min(max_dim, cat2_num));

outputs:

 -1392522416, 3, 1, 1
 2, 3, 2, 2
 3, 3, 3, 3
 3, 3, 6, 3
 3, 3, 6, 3
 -1392522416, 3, 1, 1
 3, 3, 6, 3
 2, 3, 2, 2
 -1392522416, 3, 1, 1
 -1392522416, 3, 1, 1
 3, 3, 4, 3
 -1392522416, 3, 1, 1
 -1392522416, 3, 1, 1
 -1392522416, 3, 1, 1
 3, 3, 4, 3
 3, 3, 3, 3
 2, 3, 2, 2
 -1392522416, 3, 1, 1
 2, 3, 2, 2
 -1392522416, 3, 1, 1
 -1392522416, 3, 1, 1
 -1392522416, 3, 1, 1
 2, 3, 2, 2

Aucun commentaire:

Enregistrer un commentaire