[PATCH] D23960: Avoid narrowing warnings in __bitset constructor

Dimitry Andric via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 1 15:39:52 PDT 2016


dim added inline comments.

================
Comment at: include/bitset:262
@@ -261,3 +261,3 @@
 #elif __SIZEOF_SIZE_T__ == 4
-    : __first_{__v, __v >> __bits_per_word}
+    : __first_{static_cast<__storage_type>(__v), static_cast<__storage_type>(__v >> __bits_per_word)}
 #else
----------------
EricWF wrote:
> Only the first one actually requires a cast, correct? The second should never be a potentially narrowing conversion.
No, originally I got two separate warnings for this:
```
/usr/include/c++/v1/bitset:265:16: error: non-constant-expression cannot be narrowed from type 'unsigned long long' to '__storage_type' (aka 'unsigned int') in initializer list [-Wc++11-narrowing]
    : __first_{__v, __v >> __bits_per_word}
               ^~~
/usr/include/c++/v1/bitset:676:52: note: in instantiation of member function 'std::__1::__bitset<2, 53>::__bitset' requested here
        bitset(unsigned long long __v) _NOEXCEPT : base(__v) {}
                                                   ^
contrib/llvm/include/llvm/IR/Attributes.h:455:9: note: in instantiation of member function 'std::__1::bitset<53>::bitset' requested here
      : Attrs(0), Alignment(0), StackAlignment(0), DerefBytes(0),
        ^
/usr/include/c++/v1/bitset:265:16: note: insert an explicit cast to silence this issue
    : __first_{__v, __v >> __bits_per_word}
               ^~~
/usr/include/c++/v1/bitset:265:21: error: non-constant-expression cannot be narrowed from type 'unsigned long long' to '__storage_type' (aka 'unsigned int') in initializer list [-Wc++11-narrowing]
    : __first_{__v, __v >> __bits_per_word}
                    ^~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/v1/bitset:265:21: note: insert an explicit cast to silence this issue
    : __first_{__v, __v >> __bits_per_word}
                    ^~~~~~~~~~~~~~~~~~~~~~
```



https://reviews.llvm.org/D23960





More information about the cfe-commits mailing list