vc++ 中是否存在编译器错误? (我是初学者)

Is there an compiler bug in vc++? (I am a beginner)

vc++(调试模式)和g++效果不同,用这段代码

test.hh

class Test
{
public:
    int a, b, c;
    Test(int a, int b, int c) : a{ a }, b{ b }, c{ c } {      }

    Test& operator++();
    Test  operator++(int);

};
std::ostream& operator<<(std::ostream& os, const Test& t);

test.cc

std::ostream& operator<<(std::ostream& os, const Test& t)
{
    os << "{ " << t.a << ',' << t.b << ',' << t.c << " }";
    return os;
}

Test Test::operator++(int)
{
    Test temp{ *this };
    operator++();
    return temp;
}

Test& Test::operator++()
{
    ++a; ++b; ++c;
    return *this;
}

main.cc

Test t{ 1,2,3 };

std::cout << t << '\n'
          << t++ << '\n'
          << t;

在vc++中执行的结果是

{ 2,3,4 }
{ 1,2,3 }
{ 2,3,4 }

但在 g++ 中是

{ 1,2,3 }
{ 1,2,3 }
{ 2,3,4 }

所以,vc++ 中是否存在编译器错误或我没有学到的东西。

不幸的是,<< 具有推动“排序”想法的心理作用。

虽然 std::cout << a() << b() << c() 传达了计算 a() 的想法,然后 b() 然后 c() 这是(曾经)错误的。你只知道它们会按顺序放入输出流中,但它们可以按任意顺序计算 ().

最近已修复此问题(因此您观察到的差异可能取决于您使用的 C++ 标准),但不幸的是,仅适用于常见的特殊情况,例如流输出过载的左移运算符 (IMO由于一些原因,C++ 的另一个可悲的选择。