最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

visual c++ - MSVC compiler interprets numeric literal as int instead of uint8_t - Stack Overflow

programmeradmin2浏览0评论
#include <unordered_map>

const std::unordered_map<int, uint8_t> Map{
    {10, 5},
};

int main()
{
}

Warning Level is /W4 and I have also set "treat warnings as error" to true.

Building with MSVC compiler version 19.43.34809 I get the following error:

1>ConsoleApplication1.cpp
1>C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.43.34808\include\utility(274,54): error C2220: the following warning is treated as an error
1>(compiling source file 'ConsoleApplication1.cpp')
1>C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.43.34808\include\utility(274,54): warning C4244: 'initializing': conversion from '_Ty' to '_Ty2', possible loss of data
1>C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.43.34808\include\utility(274,54): warning C4244:         with
1>C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.43.34808\include\utility(274,54): warning C4244:         [
1>C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.43.34808\include\utility(274,54): warning C4244:             _Ty=int
1>C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.43.34808\include\utility(274,54): warning C4244:         ]
1>C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.43.34808\include\utility(274,54): warning C4244:         and
1>C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.43.34808\include\utility(274,54): warning C4244:         [
1>C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.43.34808\include\utility(274,54): warning C4244:             _Ty2=uint8_t
1>C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.43.34808\include\utility(274,54): warning C4244:         ]
1>(compiling source file 'ConsoleApplication1.cpp')
1>    C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.43.34808\include\utility(274,54):
1>    the template instantiation context (the oldest one first) is
1>        C:\Software\test\Test_unordered_map_uint8_t\ConsoleApplication1\ConsoleApplication1.cpp(7,5):
1>        see reference to function template instantiation 'std::pair<const int,uint8_t>::pair<int,int,0>(_Other1 &&,_Other2 &&) noexcept' being compiled
1>        with
1>        [
1>            _Other1=int,
1>            _Other2=int
1>        ]
1>            C:\Software\test\Test_unordered_map_uint8_t\ConsoleApplication1\ConsoleApplication1.cpp(6,43):
1>            see the first reference to 'std::pair<const int,uint8_t>::pair' in 'void __cdecl `dynamic initializer for 'Map''(void)'

Basically the compiler treats number 5 as int and then complains that there is data loss to converting it to uint8_t.

I have never encountered this problem before. Compiling with MingGW 11.2.0 and using "-Wall -Wextra -Wpedantic -pedantic-errors" passes.

This is the piece of utility that produces this warning:

#if _HAS_CXX23
    template <class _Other1 = _Ty1, class _Other2 = _Ty2,
#else // ^^^ _HAS_CXX23 / !_HAS_CXX23 vvv
    template <class _Other1, class _Other2,
#endif // ^^^ !_HAS_CXX23 ^^^
        enable_if_t<conjunction_v<is_constructible<_Ty1, _Other1>, is_constructible<_Ty2, _Other2>>, int> = 0>
    constexpr explicit(!conjunction_v<is_convertible<_Other1, _Ty1>, is_convertible<_Other2, _Ty2>>)
        pair(_Other1&& _Val1, _Other2&& _Val2) noexcept(
            is_nothrow_constructible_v<_Ty1, _Other1> && is_nothrow_constructible_v<_Ty2, _Other2>) // strengthened
        : first(_STD forward<_Other1>(_Val1)), second(_STD forward<_Other2>(_Val2)) {
    }

Am I missing something or is this MSVC bug?

发布评论

评论列表(0)

  1. 暂无评论