相关疑难解决方法(0)

与std :: minmax和rvalues的结构化绑定

当使用std::minmax结构化绑定时,我遇到了一个相当微妙的错误.似乎传递的rvalues并不总是像人们期望的那样被复制.最初我T operator[]() const在自定义容器上使用a ,但它似乎与文字整数相同.

#include <algorithm>
#include <cstdio>
#include <tuple>

int main()
{
    auto [amin, amax] = std::minmax(3, 6);
    printf("%d,%d\n", amin, amax); // undefined,undefined

    int bmin, bmax;
    std::tie(bmin, bmax) = std::minmax(3, 6);
    printf("%d,%d\n", bmin, bmax); // 3,6
}
Run Code Online (Sandbox Code Playgroud)

使用GCC 8.1.1 -O1 -Wuninitialized将导致0,0打印为第一行,并且:

warning: ‘<anonymous>’ is used uninitialized in this function [-Wuninitialized]
Run Code Online (Sandbox Code Playgroud)

Clang 6.0.1 at -O2也会在没有警告的情况下给出错误的第一个结果.

-O0GCC给出正确的结果而没有警告.对于clang,结果似乎是正确的-O1-O0.

在rvalue仍然有效被复制的意义上,第一行和第二行不应该是等价的吗?

另外,为什么这取决于优化级别?特别是我对GCC没有发出任何警告感到惊讶.

c++ rvalue c++17 structured-bindings

19
推荐指数
2
解决办法
725
查看次数

如果std :: max()通过引用返回(必须),那么可能导致悬空引用吗?

考虑范例max模板函数,std::max():

// From the STL

    // TEMPLATE FUNCTION _Debug_lt
template<class _Ty1, class _Ty2> inline
    bool _Debug_lt(const _Ty1& _Left, const _Ty2& _Right,
        _Dbfile_t _File, _Dbline_t _Line)
    {   // test if _Left < _Right and operator< is strict weak ordering
        if (!(_Left < _Right))
            return (false);
        else if (_Right < _Left)
            _DEBUG_ERROR2("invalid operator<", _File, _Line);
        return (true);
    }

// intermediate #defines/templates skipped 

    // TEMPLATE FUNCTION max
template<class _Ty> inline
    const _Ty& (max)(const _Ty& _Left, const _Ty& _Right)
    { …
Run Code Online (Sandbox Code Playgroud)

c++ templates

16
推荐指数
2
解决办法
2268
查看次数

使用'auto'和std :: minmax观察奇怪的行为

我在SUSE Enterprise Linux 11上使用GCC 4.7.2和Boost 1.58.0.我有以下代码片段,它基本上通过一个多边形列表来计算它们的长度/宽度.当使用带有std :: minmax函数的'auto'关键字时,我看到了奇怪的输出.为了比较,我还声明了第二个变量,其中明确声明了类型(即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 = …
Run Code Online (Sandbox Code Playgroud)

c++ auto c++11 boost-polygon

10
推荐指数
1
解决办法
442
查看次数

为什么结构化绑定仅适用于auto

结构化绑定已经与c ++ 17一起引入.它们能够声明从元组或结构初始化的多个变量.

此代码使用c++17编译器进行编译.

#include <iostream>
#include <tuple>

int main() {
    auto tuple = std::make_tuple(1.0, 1);

    auto [ d, i ] = tuple;

    std::cout << "d=" << d << " i=" << i <<  '\n';

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

如果我没有声明变量auto我得到错误

错误:lambda表达式的预期体 [d2,i2] =元组;

#include <iostream>
#include <tuple>

int main() {
    auto tuple = std::make_tuple(1.0, 2);

    double d2;
    int i2;

    [d2 , i2] = tuple;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我使用clang version 4.0.0和编译选项-std=c++1z.

我可以将现有变量分配给结构化绑定吗?我需要使用auto …

c++ auto c++17 structured-bindings

8
推荐指数
2
解决办法
1236
查看次数

小于void指针的比较

我想比较两个这样的void指针:

void foo(void* p1, void* p2) {

  if (p1 < p2) {
    void *tmp = p1;
    p1 = p2;
    p2 = tmp;
  }

  // do something with p1 and p2.
}
Run Code Online (Sandbox Code Playgroud)

根据标准,这是正确的吗?我的意思是void指针的比较是一个明确定义的行为?

如果有人能指出我所记录的C标准,我将不胜感激.

c comparison pointers

4
推荐指数
2
解决办法
2530
查看次数