MSVC 19.11 / Visual C ++ 2017: the list of initializers of size 1 and size_t are misinterpreted

Compiling the following code with MSVC 19.11 leads to the conclusion

With 32: 0 99 2 With 64: 0 1 2 with a 32-bit compiler and

With 32: 0 1 2 With 64: 0 99 2 with a 64-bit compiler.

The problem is that the list of initializers for individual elements is of type size_t. Is this a compiler error (until I found it still not reported anywhere), and not the case where the standard is ambiguous (neither clang nor gcc have this problem)?

#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;
}

Command and compiler (s):

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\
+4
source share

Source: https://habr.com/ru/post/1686135/


All Articles