加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

在c 11风格中输入铸造的正确方法?

发布时间:2020-12-16 10:36:40 所属栏目:百科 来源:网络整理
导读:我有一个这样的课: class A { void init(int a){ _data.resize(a); //! (1) } //other parts of the code are not important ...private: std::liststd::vectordouble _data;} 在(1)中,我收到了警告: implicit conversion from int to size_type(aka unsig
我有一个这样的课:

class A {
    void init(int a){
    _data.resize(a); //! (1)
    }

    //other parts of the code are not important ...
private:
    std::list<std::vector<double>> _data;
}

在(1)中,我收到了警告:

implicit conversion from int to size_type(aka unsigned long)

我想知道摆脱那个警告的正确方法是什么?
也许是这样的:

_data.resize(static_cast<decltype(_data)::size_type>(a)

注意:我猜代码应该改为:

init(size_t a)

但是我们假设我们不能改变类接口.

解决方法

您的示例演员以正确的方式执行:

>它明确表示了施放的意图.
>它转换为正确的类型,并受到保护,以防将来的_data类型更改.
>它使用正确的演员.

如果改变界面是不可能的,那就坚持下去吧.

在使用它之前,最好为输入添加健全性检查.在将其转换为unsigned之前检查负值是最基本和最有用的值.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读