How to cast the size_t to double or int C++

前端 未结 5 857
旧时难觅i
旧时难觅i 2020-12-25 09:48

My question is that

I have a size_t data, but now I want to convert it to double or int.

If I do something like

 size_t data = 99999999;
 in         


        
5条回答
  •  礼貌的吻别
    2020-12-25 10:29

    If your code is prepared to deal with overflow errors, you can throw an exception if data is too large.

    size_t data = 99999999;
    if ( data > INT_MAX )
    {
       throw std::overflow_error("data is larger than INT_MAX);
    }
    int convertData = static_cast(data);
    

提交回复
热议问题