MSVC 19.11 / Visual C 2017:尺寸1和size_t类型的初始化列表误解



用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)
x64cl.exe and x86cl.exe from
...ToolsMSVC14.11.25503binHostX64

是的,这是一个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及其两个构造函数。

根据over.ics.rank#3.1

列表启动序列L1比列表限制序列L2更好的转换序列L2(3.1.1)L1转换为某些X和L2的pinitiber_list dinitizer_list没有...

因此,在此示例中,V({size_t(3)})应调用构造函数V(std::initializer_list<int> l),并且在GCC和CLANG中这样做,但在MSVC中不做。演示:https://gcc.godbolt.org/z/3q64f7yr7

MSVC错误报告:https://develovelerCommunity.visualstudio.com/t/wrong-comstructor-er-std:: vector-selecte/1652923

相关内容

最新更新