C ++隐式转换基元的警告或错误 [英] Warnings or errors for C++ implicit conversion of primitives

查看:212
本文介绍了C ++隐式转换基元的警告或错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对一些C ++代码做了一些重构,发现了许多我不知道的隐式转换错误。



示例



  struct A * a 

bool b(){
return a();
}

void c(){
int64_t const d(b());
}



问题




  1. b 中, a 的返回类型默认转换为 code>默认提升为 int64_t



h2>

如何接收原始类型之间的隐式转换的警告或错误



注意




  1. 使用 -Wconversion 似乎只会拾取几个不相关的

  2. BOOST_STRONG_TYPEDEF 不是选项(我的类型需要是POD,因为它们在磁盘结构中使用)。

  3. C也很有趣, 在 C ++编程语言,第3版,附录C.6,即隐式类型转换,Bjarne Stroustrup将转换分类为促销转换:第一个保留值(这是您的情况2),第二个不会(情况1)。



    关于转换,他说基本类型可以以令人困惑的方式转换成彼此。在我看来,允许太多的转换。和编译器可以警告许多有问题的转换。幸运的是,许多编译器实际上是做的。



    另一方面的促销是安全的,看起来像一个编译器不应该给警告。



    编译器警告通常不是强制性的。通常在C ++ 草稿和最终的ANSI文档,报告实施者应该发出警告建议:如果需要,您可以自己检查更多信息。



    EDITED:added C ++ 11 note:



    第三版的附录C ++编程语言第四版已被报告并再次作为第10.5节隐式类型转换扩展。



    由于前面的考虑是一样的,C ++ 11更精确地定义了缩小转换,并添加了{} -initializer表示法(6.3.5),截断导致编译错误。


    I've done some heavy refactoring of some C++ code, and discovered numerous bugs arising from implicit conversions that I'm not aware of.

    Example

    struct A *a();
    
    bool b() {
        return a();
    }
    
    void c() {
        int64_t const d(b());
    }
    

    Issues

    1. In b, the return type of a is silently cast to bool.
    2. In c, the value returned from b is silently promoted to int64_t.

    Question

    How can I receive warnings or errors for the implicit conversion between primitive types?

    Note

    1. The use of -Wconversion seems to only pick up several arbitrary conversions unrelated to the example above.
    2. BOOST_STRONG_TYPEDEF is not an option (my types need to be PODs, as they're used in disk structures).
    3. C is also of interest, however this problem pertains to a C++ code base.

    解决方案

    In the C++ programming language, 3rd edition, appendix C.6, namely "Implicit Type Conversion", Bjarne Stroustrup classifies conversions as promotions and conversions: the first ones "preserve values" (that's your case 2), the second ones doesn't (case 1).

    About conversions, he says that "The fundamental types can be converted into each other in a bewildering number of ways. In my opinion, too many conversions are allowed." and "A compiler can warn about many questionable conversions. Fortunately, many compilers actually do."

    promotions on the other side are safe, and it seems like a compiler is not supposed to give a warning for them.

    Compiler warnings are usually not mandatory. Usually in the C++ drafts and final ANSI documents it is reported that "implementers should issue a warning" where suggested: you can check it yourself for further information if needed.

    EDITED: added C++11 note:

    In The C++ programming language, 4th edition, the appendix of the 3rd edition has been reported and extended as section 10.5, "Implicit Type Conversion" again.

    Being the previous considerations the same, C++11 more precisely define "narrowing conversions" and adds up the {}-initializer notation (6.3.5), with which truncations lead to a compilation error.

    这篇关于C ++隐式转换基元的警告或错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆