本文介绍了使用 'auto' 和 std::minmax 观察奇怪的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我在 SUSE Enterprise Linux 11 上使用 GCC 4.7.2 和 Boost 1.58.0.我有以下代码片段,它基本上通过多边形列表来计算它们的长度/宽度.在 std::minmax 函数中使用 'auto' 关键字时,我看到了奇怪的输出.为了进行比较,我还声明了第二个变量,其中明确声明了类型(即,dim 与 dim1).

I am using GCC 4.7.2 and Boost 1.58.0 on SUSE Enterprise Linux 11. I have the following code snippet which basically goes through a list of polygons to compute their length/width. I'm seeing strange output when using the 'auto' keyword with the std::minmax function. To compare, I also declare a second variable where the types are explicitly declared (i.e., dim vs dim1).

namespace gtl = boost::polygon;
typedef gtl::polygon_90_data<int> LayoutPolygon;
typedef gtl::rectangle_data<int> LayoutRectangle;
static LayoutFeatureVec
calc_stats(LayoutPolygonSet const& lp)
{
   LayoutFeatureVec v;
   LayoutFeature f;
   LayoutRectangle y;
   for (LayoutPolygon const& p : lp) {
      // Compute bounds.
      gtl::extents(y, p);

      // Get width/length (shorter/longer).
      // FIXME: Why does this not work with auto??
      cout << gtl::delta(y, gtl::HORIZONTAL) << " " << gtl::delta(y, gtl::VERTICAL) << endl;

      auto dim = std::minmax(gtl::delta(y, gtl::HORIZONTAL),
                             gtl::delta(y, gtl::VERTICAL));

      std::pair<int, int> dim1 = std::minmax(gtl::delta(y, gtl::HORIZONTAL),
                                             gtl::delta(y, gtl::VERTICAL));

      cout << dim.first << " " << dim.second << endl;
      cout << dim1.first << " " << dim1.second << endl;

      <snip>
      v.push_back(f);
   }

   return v;
}

对于这个循环的第一次迭代,预期的输出是正确的.

For the first iteration of this loop, the expected output is this which is correct.

380 420
380 420
380 420

但是,如果我注释掉 'dim1' 并重新运行相同的代码(即,只需自动变暗),我会得到 std::minmax 的奇怪结果.

However, if I comment out 'dim1' and rerun the same code (i.e., just have auto dim), I get weird results with std::minmax.

380 420
140737295994126 140737295994126

我在这里做错了什么?

这是最小的示例(根据下面的答案进行编辑).

Here is the minimal example (edited based on answer below).

#include <iostream>
#include <algorithm>
#include <boost/polygon/polygon.hpp>

using namespace std;

namespace gtl = boost::polygon;
using namespace gtl::operators;

int main(int argc, char** argv)
{
    gtl::rectangle_data<int> x(0,0,5,5);

    auto dim = std::minmax(gtl::delta(x, gtl::HORIZONTAL), gtl::delta(x, gtl::VERTICAL));
    cout << dim.first << " " << dim.second << endl;

    return 0;
}

推荐答案

这是使用 auto 作为类型说明符的情况之一.std::minmax 返回一对 参考:

This is one of those cases of where not to use auto as the type specifier. std::minmax returns a pair of references:

template< class T >
std::pair<const T&,const T&> minmax( const T& a, const T& b );

这就是 auto 会推断出来的.但是 delta() 返回一个临时的.所以当你写:

That's what auto will deduce. But delta() returns a temporary. So when you write:

auto dim = std::minmax(gtl::delta(y, gtl::HORIZONTAL),
                       gtl::delta(y, gtl::VERTICAL));

dim 持有两个悬空引用.但是当你写:

dim is holding two dangling references. But when you write:

std::pair<int, int> dim1 = std::minmax(...);

您只是直接持有值.这就是为什么这有效但 auto 无效的原因.您正在执行的额外转换可防止您持有悬空引用.

You're just holding the values directly. That's why this works but auto doesn't. The extra conversion you're performing prevents you from holding dangling references.

或者,为了完整性,您可以使用不返回引用的不同的 minmax 重载:

Alternatively, and for completeness, you could use a different overload of minmax that doesn't return references:

template< class T >
std::pair<T,T> minmax( std::initializer_list<T> ilist);

这只是涉及一些额外的大括号:

which just involves some extra braces:

auto dim2 = std::minmax({gtl::delta(y, gtl::HORIZONTAL),
                         gtl::delta(y, gtl::VERTICAL)});

但我建议只明确命名类型.这对我来说似乎不太容易出错.

But I'd suggest just explicitly naming the type. That seems less error-prone to me.

这篇关于使用 'auto' 和 std::minmax 观察奇怪的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 09:12