auto open_flags = std::ios::binary; seems to produce the wrong type in MSVC. Is it a bug?

huangapple go评论64阅读模式
英文:

auto open_flags = std::ios::binary; seems to produce the wrong type in MSVC. Is it a bug?

问题

这段C++代码在gcc、icc和clang下编译正常,但在MSVC下失败:

#include <ios>

int main()
{
    auto open_flags = std::ios::binary;
    open_flags |= std::ios::app;

    return 0;
}

错误信息为:<source>(6): error C2678: binary '|=': no operator found which takes a left-hand operand of type 'std::_Iosb<int>::_Openmode' (or there is no acceptable conversion)

将代码更改为以下形式会得到更有帮助的错误信息:

#include <ios>

int main()
{
    auto open_flags = std::ios::binary;
    open_flags = open_flags | std::ios::app;

    return 0;
}

错误信息为:<source>(6): error C2440: '=': cannot convert from 'int' to 'std::_Iosb<int>::_Openmode'

而下面的代码编译正常:

#include <ios>

int main()
{
    auto open_flags = std::ios::binary | std::ios::out;
    open_flags = open_flags | std::ios::app;

    return 0;
}

这看起来像是不正确的行为。似乎MSVC已将|运算符的返回类型实现为int,而不是ios::openmode

值得注意的是,如果我使用std::ios::openmode而不是auto,原始代码会编译通过,可能是通过隐式转换实现的。

这是否是MSVC的错误,还是我漏掉了什么?欢迎参考标准文档!

英文:

This c++ code compiles fine with gcc, icc, and clang, but fails with MSVC:

#include &lt;ios&gt;

int main()
{
    auto open_flags = std::ios::binary;
    open_flags |= std::ios::app;

    return 0;
}

> <source>(6): error C2678: binary '|=': no operator found which takes a left-hand operand of type 'std::_Iosb<int>::_Openmode' (or there is no acceptable conversion)
https://godbolt.org/z/999fffPEx

Changing the code to this gives a more helpful error message:

#include &lt;ios&gt;

int main()
{
    auto open_flags = std::ios::binary;
    open_flags = open_flags | std::ios::app;

    return 0;
}

> <source>(6): error C2440: '=': cannot convert from 'int' to 'std::_Iosb<int>::_Openmode'

And this compiles fine:

#include &lt;ios&gt;

int main()
{
    auto open_flags = std::ios::binary | std::ios::out;
    open_flags = open_flags | std::ios::app;

    return 0;
}

This looks like incorrect behaviour to me. Like MSVC has implemented the | operator with return type int instead of ios::openmode.

It's also worth noting that the original code compiles if I use std::ios::openmode instead of auto, presumably through implicit conversion.

Is this an MSVC bug, or am I missing something? Standards references welcome!

答案1

得分: 4

这是一个 MSVC 的错误。

根据标准:

  1. binary 具有类型 std::ios_base::openmodeios.base.general);
  2. std::ios_base::openmode 是一个位掩码类型(ios.openmode)。

但在 MSVC 上:

  1. binary 具有枚举类型(内部类型 _Openmode),但 std::ios_base::openmodeint
  2. _Openmode 不是位掩码类型(因此只能使用内置运算符 |, &, ^, ~)。

请在 https://github.com/microsoft/STL 提交一个错误报告(或两个)。

英文:

It's a MSVC bug.

According to the standard:

  1. binary has type std::ios_base::openmode ([ios.base.general]);
  2. std::ios_base::openmode is a bitmask type ([ios.openmode]).

But on MSVC:

  1. binary has an enum type (the internal type _Openmode), but std::ios_base::openmode is int.
  2. _Openmode is not a bitmask type (and thus only built-in operators |, &amp;, ^, ~ are applicable).

Please file a bug report (or two) at https://github.com/microsoft/STL.

huangapple
  • 本文由 发表于 2023年2月10日 11:41:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75406709.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定