Visual C ++ 2017的bug?编译器优化了表达式

时间:2018-02-28 18:49:19

标签: c++ visual-c++ visual-studio-2017 compiler-bug

下面这段代码给我带来了一些问题,特别是diff的评估:

#include <iostream>

template<typename T>
class array2d
{
protected:
    T* arr;
    int w, h;

public:
array2d() : arr(nullptr), w(0), h(0) {}

bool init(int w, int h)
{
    free();
    arr = new (std::nothrow) T[w*h];
    if (arr == nullptr)
    {
        std::cerr << "array2d::init(): Failed to allocate: " << w * h * sizeof(T) << " bytes.\n";
        return false;
    }
    this->w = w;
    this->h = h;
    return true;
}

void fill(T v)
{
    for (int i = 0; i < w*h; ++i)
        arr[i] = v;
}

void free()
{
    if (arr != nullptr)
    {
        delete[] arr;
        arr = nullptr;
        w = 0;
        h = 0;
    }
}

template<typename U>
bool copy(const array2d<U>& v)
{
    if (!v)
    {
        std::cerr << "array2d::copy(): Input array is empty.\n";
        return false;
    }
    if (w*h != v.width()*v.height())
    {
        if (!init(v.width(), v.height())) return false;
    }
    for (int i = 0; i < w*h; ++i)
        arr[i] = static_cast<T>(v(i));
    return true;
}

inline T operator()(int i) const { return arr[i]; }
inline T& operator()(int i) { return arr[i]; }
inline T operator[](int i) const { return arr[i]; }
inline T& operator[](int i) { return arr[i]; }
inline T operator()(int x, int y) const { return arr[x + y*w]; }
inline T& operator()(int x, int y) { return arr[x + y*w]; }

inline int width() const { return w; }
inline int height() const { return h; }
inline const T* get() const { return arr; }
inline T* get() { return arr; }

operator bool() const { return arr != nullptr; }

array2d<T>& operator*=(T v)
{
    for (int i = 0; i < w*h; ++i)
        arr[i] *= v;
    return *this;
}

~array2d() { free(); }
};

typedef unsigned long long uint64;

uint64 computeErrors(const array2d<uint64>& a, const array2d<uint64>& b)
{
    uint64 MNE = 0;
    for (int i = 0; i < a.height()*a.width(); ++i)
    {
        uint64 diff = ((a[i] >= b[i]) ? a[i] - b[i] : b[i] - a[i]);
        if (a[i] != b[i])
        {
            std::cout << "Diff: " << diff << "\n";
            std::cout << "What diff should equal to: " << ((a[i] >= b[i]) ? a[i] - b[i] : b[i] - a[i]) << "\n";
        }
        if (i == 0)
        {
            std::cout << "Diff should not be 0, but it is: " << diff << "\n";
        }
        if (MNE < diff)
        {
            MNE = diff;
            std::cout << "We should reach this, but we don't.\n";
        }
    }
    return MNE;
}



int main()
{
    int w = 1;
    int h = 1;
    array2d<uint64> a;
    if (!a.init(w,h)) return 1;
    a.fill(0);
    array2d<uint64> b;
    if (!b.init(w,h)) return 1;
    b.fill(0);
    a[0] = 0ull;
    b[0] = 1;
    std::cout << a[0] << " " << b[0] << "\n";
    auto e = computeErrors(a, b);
    std::cout << "MNE: " << e << "\n";

    return 0;
}

如果我用/ O1,/ O2或/ Ox编译它;使用/ Ot和/ Ob2,然后diff总是0,当它不应该是。如果我在没有/ Ot或没有/ Ob2的情况下编译,一切都按预期工作。这段代码中有什么不正确的吗?我在这里附上了全部内容:http://coliru.stacked-crooked.com/a/245f23f05df39418。如果我更改数组&#39; s []运算符以返回const T&amp;然而,不是T,一切正常。这是编译器错误吗?或者是我的糟糕回归T而不是const T&amp;在operator []中(我正在返回T,因为我读到了某个地方,按值传递较小的POD更快,在我的情况下,我有一些带有uint32的数组,其中速度非常重要)。我目前正在使用Visual C ++ 2017.

编辑: 根据用户4581301的建议,我也在这里添加了整个代码。

EDIT2: 这是我的_MSC_FULL_VER:191125547。

1 个答案:

答案 0 :(得分:1)

该报告已于2018年2月28日提交给Visual Studio Developer Community(我由@lightxbulb承担)。

2018年5月8日,据报道(相同链接),该问题已在最新的Visual Studio版本2017 15.7中得以解决(根据发布标头中的信息)。