Why does a narrowing conversion warning appear only in case of list initialization?

后端 未结 3 544
暖寄归人
暖寄归人 2021-01-04 02:17

I have the following code:

class A
{
    public:
        A(const unsigned int val) : value(val) {}

        unsigned int value;
};

int main()
{
    int val          


        
3条回答
  •  南方客
    南方客 (楼主)
    2021-01-04 02:30

    list initialization was introduced since C++11 with the feature prohibiting implicit narrowing conversions among built-in types. At the same time, the other two "old-style" (since C++98) initialization forms which use parentheses and equal-sign like

    int val = 42;
    A a(val);
    A a = val;
    

    don't change their behavior to accord with list initialization, because that could break plenty of legacy code bases.

提交回复
热议问题