C++ 0x sẽ làm cho mã sau và mã tương tự không đúng định dạng, vì nó yêu cầu một chuyển đổi thu hẹp thu hẹp của một double
thành một số int
.Thu hẹp chuyển đổi trong C++ 0x. Chỉ là tôi, hay âm thanh này như một sự thay đổi đột ngột?
int a[] = { 1.0 };
Tôi tự hỏi liệu kiểu khởi tạo này có được sử dụng nhiều trong mã thế giới thực hay không. Có bao nhiêu mã sẽ bị phá vỡ bởi thay đổi này? Có rất nhiều nỗ lực để sửa lỗi này trong mã của bạn, nếu mã của bạn bị ảnh hưởng chút nào?
Để tham khảo, xem 8.5.4/6 n3225
A narrowing conversion is an implicit conversion
- from a floating-point type to an integer type, or
- from long double to double or float, or from double to float, except where the source is a constant expression and the actual value after conversion is within the range of values that can be represented (even if it cannot be represented exactly), or
- from an integer type or unscoped enumeration type to a floating-point type, except where the source is a constant expression and the actual value after conversion will fit into the target type and will produce the original value when converted back to the original type, or
- from an integer type or unscoped enumeration type to an integer type that cannot represent all the values of the original type, except where the source is a constant expression and the actual value after conversion will fit into the target type and will produce the original value when converted back to the original type.
cho phép hy vọng không, tôi không thấy loại này khởi tạo nhưng họ yên tâm rằng họ đang cố gắng tốt nhất của họ để không phá vỡ bất kỳ mã-base , C++ 0x có hiệu ứng tốt, không có ít hơn –
@litb: Điều này cũng sẽ bị hỏng, hay chỉ là khi có chuyển đổi xảy ra? 'int a [10] = {0};' –
Giả sử điều này là hợp lệ chỉ để khởi tạo các loại sẵn có, tôi không thể thấy điều này sẽ gây hại như thế nào. Chắc chắn, điều này có thể phá vỡ một số mã. Nhưng nên dễ sửa. –