英文:
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 <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)
https://godbolt.org/z/999fffPEx
Changing the code to this gives a more helpful error message:
#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'
And this compiles fine:
#include <ios>
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 的错误。
根据标准:
binary
具有类型std::ios_base::openmode
(ios.base.general);std::ios_base::openmode
是一个位掩码类型(ios.openmode)。
但在 MSVC 上:
binary
具有枚举类型(内部类型_Openmode
),但std::ios_base::openmode
是int
。_Openmode
不是位掩码类型(因此只能使用内置运算符|
,&
,^
,~
)。
请在 https://github.com/microsoft/STL 提交一个错误报告(或两个)。
英文:
It's a MSVC bug.
According to the standard:
binary
has typestd::ios_base::openmode
([ios.base.general]);std::ios_base::openmode
is a bitmask type ([ios.openmode]).
But on MSVC:
binary
has an enum type (the internal type_Openmode
), butstd::ios_base::openmode
isint
._Openmode
is not a bitmask type (and thus only built-in operators|
,&
,^
,~
are applicable).
Please file a bug report (or two) at https://github.com/microsoft/STL.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论