使用 MSVC 19.11 编译以下代码会产生输出
With 32: 0 99 2 With 64: 0 1 2
使用 32 位编译器,并在
With 32: 0 1 2 With 64: 0 99 2
使用 64 位编译器。
问题在于单元素初始值设定项列表正是
size_t
类型。这是一个编译器错误(到目前为止我还没有发现它在任何地方报告过),而不是标准不明确的情况(clang 和 gcc 都没有这个问题)?
#include <cstdint>
#include <vector>
#include <iostream>
int main() {
using T = std::uint16_t;
// fixed with uint32 / uint64 on 32 / 64 bit compilers, respectively,
// but not with int32_t / int64_t
{
std::vector<T> s0;
// std::vector<T> s1{ 99u }; // OK
// std::vector<T> s1 = { 99u }; // OK
std::vector<T> s1( { 99u } ); // BUG?
// EDIT: std::vector<T> s1( {{ 99u }} ); // also OK
std::vector<T> s2( { 40u, 70u } );
std::cout << "With " << sizeof(0u)*8 << ':' << ' '
<< s0.size() << ' ' << s1.size() << ' ' << s2.size() << '\n';
}
{
std::vector<T> s0;
std::vector<T> s1( { 99ull } );
std::vector<T> s2( { 40ull, 70ull } );
std::cout << "With " << sizeof(0ull)*8 << ':' << ' '
<< s0.size() << ' ' << s1.size() << ' ' << s2.size() << '\n';
}
return 0;
}
命令和编译器:
cl.exe ilist.cpp & .\ilist.exe # no extra cl arguments
cl.exe
Microsoft (R) C/C++ Optimizing Compiler Version 19.11.25507.1 for x64
Copyright (C) Microsoft Corporation. All rights reserved. (or x86)
x64\cl.exe and x86\cl.exe from
...\Tools\MSVC\14.11.25503\bin\HostX64\
是的,这是 Visual Studio 编译器错误。
一个更简单的例子来说明问题:
#include <initializer_list>
#include <cstddef>
struct V {
size_t sz;
constexpr V(size_t s) { sz = s; }
constexpr V(std::initializer_list<int> l) { sz = l.size(); }
};
static_assert( V({size_t(3)}).sz == 1 );
这里
struct V
模仿 std::vector
及其两个构造函数。
列表初始化序列 L1 是比列表初始化序列 L2 更好的转换序列,如果 (3.1.1) 对于某些 X,L1 会转换为 std::initializer_list,而 L2 不会...
所以在这个例子中
V({size_t(3)})
应该调用构造函数V(std::initializer_list<int> l)
,并且在GCC和Clang中这样做,但在MSVC中则不然。演示:https://gcc.godbolt.org/z/3q64f7Yr7
此 MSVC 错误已在 Visual Studio 2022 版本 17.9 中修复:https://developercommunity.visualstudio.com/t/Wrong-constructor-of-std::vector-selecte/1652923