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

后端 未结 3 557
暖寄归人
暖寄归人 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:45

    The reason behind the warning is already explained by other answers.

    This is how to fix this warning/error. Create a constructor which takes initializer_list as argument.

    A(std::initializer_list l) : value(*(l.begin())) { 
        cout << "constructor taking initializer list called\n";
    }
    

提交回复
热议问题