[PATCH] D23960: Avoid narrowing warnings in __bitset constructor

Dimitry Andric via cfe-commits cfe-commits at lists.llvm.org
Sat Aug 27 07:52:08 PDT 2016


dim created this revision.
dim added reviewers: mclow.lists, EricWF.
dim added subscribers: emaste, cfe-commits.

When I compile <bitset> is compiled with warnings enabled, I get the
following error (which is interesting in itself, it should only be a
warning):

    /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) {}
                                                       ^
    /home/dim/src/llvm/trunk/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}
                   ^~~

Note that this is on i386, so this uses the `#elif` part of the
`__bitset` constructor:

    __bitset<_N_words, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
    #ifndef _LIBCPP_HAS_NO_CONSTEXPR
    #if __SIZEOF_SIZE_T__ == 8
        : __first_{__v}
    #elif __SIZEOF_SIZE_T__ == 4
        : __first_{__v, __v >> __bits_per_word}
    #else
    #error This constructor has not been ported to this platform
    #endif
    #endif

Indeed, `__first_` has type `__storage_type`, which is 32 bits on this
platform, so I think an explicit static_cast is required here.

https://reviews.llvm.org/D23960

Files:
  include/bitset

Index: include/bitset
===================================================================
--- include/bitset
+++ include/bitset
@@ -259,7 +259,7 @@
 #if __SIZEOF_SIZE_T__ == 8
     : __first_{__v}
 #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
 #error This constructor has not been ported to this platform
 #endif


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D23960.69489.patch
Type: text/x-patch
Size: 452 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160827/b548b111/attachment.bin>


More information about the cfe-commits mailing list