Narrowing conversions in C++0x. Is it just me, or does this sound like a breaking change?

后端 未结 8 601
无人共我
无人共我 2020-11-27 14:21

C++0x is going to make the following code and similar code ill-formed, because it requires a so-called narrowing conversion of a double to a int<

相关标签:
8条回答
  • 2020-11-27 14:52

    It was indeed a breaking change as real life experience with this feature has shown gcc had turned narrowing into a warning from an error for many cases due to real life pains with porting C++03 code bases to C++11. See this comment in a gcc bug report:

    The standard only requires that "a conforming implementation shall issue at least one diagnostic message" so compiling the program with a warning is allowed. As Andrew said, -Werror=narrowing allows you to make it an error if you want.

    G++ 4.6 gave an error but it was changed to a warning intentionally for 4.7 because many people (myself included) found that narrowing conversions where one of the most commonly encountered problems when trying to compile large C++03 codebases as C++11. Previously well-formed code such as char c[] = { i, 0 }; (where i will only ever be within the range of char) caused errors and had to be changed to char c[] = { (char)i, 0 }

    0 讨论(0)
  • 2020-11-27 14:54

    Try adding -Wno-narrowing to your CFLAGS, for example :

    CFLAGS += -std=c++0x -Wno-narrowing
    
    0 讨论(0)
  • 2020-11-27 14:55

    I wouldn't be all that surprised if somebody gets caught out by something like:

    float ra[] = {0, CHAR_MAX, SHORT_MAX, INT_MAX, LONG_MAX};
    

    (on my implementation, the last two don't produce the same result when converted back to int/long, hence are narrowing)

    I don't remember ever writing this, though. It's only useful if an approximation to the limits is useful for something.

    This seems at least vaguely plausible too:

    void some_function(int val1, int val2) {
        float asfloat[] = {val1, val2};    // not in C++0x
        double asdouble[] = {val1, val2};  // not in C++0x
        int asint[] = {val1, val2};        // OK
        // now do something with the arrays
    }
    

    but it isn't entirely convincing, because if I know I have exactly two values, why put them in arrays rather than just float floatval1 = val1, floatval1 = val2;? What's the motivation, though, why that should compile (and work, provided the loss of precision is within acceptable accuracy for the program), while float asfloat[] = {val1, val2}; shouldn't? Either way I'm initializing two floats from two ints, it's just that in one case the two floats happen to be members of an aggregate.

    That seems particularly harsh in cases where a non-constant expression results in a narrowing conversion even though (on a particular implementation), all values of the source type are representable in the destination type and convertible back to their original values:

    char i = something();
    static_assert(CHAR_BIT == 8);
    double ra[] = {i}; // how is this worse than using a constant value?
    

    Assuming there's no bug, presumably the fix is always to make the conversion explicit. Unless you're doing something odd with macros, I think an array initializer only appears close to the type of the array, or at least to something representing the type, which could be dependent on a template parameter. So a cast should be easy, if verbose.

    0 讨论(0)
  • 2020-11-27 15:02

    Narrowing conversion errors interact badly with implicit integer promotion rules.

    I had an error with code which looked like

    struct char_t {
        char a;
    }
    
    void function(char c, char d) {
        char_t a = { c+d };
    }
    

    Which produces an narrowing conversion error (which is correct according to the standard). The reason is that c and d implicitly get promoted to int and the resulting int isn't allowed to be narrowed back to char in an initializer list.

    OTOH

    void function(char c, char d) {
        char a = c+d;
    }
    

    is of course still fine (otherwise all hell would break loose). But surprisingly, even

    template<char c, char d>
    void function() {
        char_t a = { c+d };
    }
    

    is ok and compiles without a warning if the sum of c and d is less than CHAR_MAX. I still think this is a defect in C++11, but the people there think otherwise - possibly because it isn't easy to fix without get rid of either implicit integer conversion (which is a relict from the past, when people wrote code like char a=b*c/d and expected it to work even if (b*c) > CHAR_MAX) or narrowing conversion errors (which are possibly a good thing).

    0 讨论(0)
  • 2020-11-27 15:05

    I would be surprised and disappointed in myself to learn that any of the C++ code I wrote in the last 12 years had this sort of problem. But most compilers would have spewed warnings about any compile-time "narrowings" all along, unless I'm missing something.

    Are these also narrowing conversions?

    unsigned short b[] = { -1, INT_MAX };
    

    If so, I think they might come up a bit more often than your floating-type to integral-type example.

    0 讨论(0)
  • 2020-11-27 15:12

    It looks like GCC-4.7 no longer gives errors for narrowing conversions, but warnings instead.

    0 讨论(0)
提交回复
热议问题