[PATCH] D27268: [libcxx] [test] Fix MSVC x64 warning C4267 "conversion from 'size_t' to 'int' [or 'unsigned int'], possible loss of data", part 2/4.

Stephan T. Lavavej via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 30 10:03:31 PST 2016


STL_MSFT created this revision.
STL_MSFT added reviewers: EricWF, mclow.lists.
STL_MSFT added a subscriber: cfe-commits.

[libcxx] [test] Fix MSVC x64 warning C4267 "conversion from 'size_t' to 'int' [or 'unsigned int'], possible loss of data", part 2/4.

Use static_cast<int> when storing size_t in int (or passing size_t to int).

Also, remove a spurious semicolon in test/support/archetypes.hpp.


https://reviews.llvm.org/D27268

Files:
  test/std/utilities/optional/optional.specalg/make_optional_explicit_initializer_list.pass.cpp
  test/support/archetypes.hpp
  test/support/count_new.hpp


Index: test/support/count_new.hpp
===================================================================
--- test/support/count_new.hpp
+++ test/support/count_new.hpp
@@ -79,7 +79,7 @@
         }
         ++new_called;
         ++outstanding_new;
-        last_new_size = s;
+        last_new_size = static_cast<int>(s);
     }
 
     void deleteCalled(void * p)
@@ -101,7 +101,7 @@
         }
         ++outstanding_array_new;
         ++new_array_called;
-        last_new_array_size = s;
+        last_new_array_size = static_cast<int>(s);
     }
 
     void deleteArrayCalled(void * p)
@@ -304,10 +304,10 @@
     void requireExactly(std::size_t N) { m_req_alloc = N; m_exactly = true; }
 
     ~RequireAllocationGuard() {
-        assert(globalMemCounter.checkOutstandingNewEq(m_outstanding_new_on_init));
+        assert(globalMemCounter.checkOutstandingNewEq(static_cast<int>(m_outstanding_new_on_init)));
         std::size_t Expect = m_new_count_on_init + m_req_alloc;
-        assert(globalMemCounter.checkNewCalledEq(Expect) ||
-               (!m_exactly && globalMemCounter.checkNewCalledGreaterThan(Expect)));
+        assert(globalMemCounter.checkNewCalledEq(static_cast<int>(Expect)) ||
+               (!m_exactly && globalMemCounter.checkNewCalledGreaterThan(static_cast<int>(Expect))));
     }
 
 private:
Index: test/support/archetypes.hpp
===================================================================
--- test/support/archetypes.hpp
+++ test/support/archetypes.hpp
@@ -69,11 +69,11 @@
     }
     template <bool Dummy = true, typename std::enable_if<Dummy && Explicit, bool>::type = true>
     explicit TestBase(std::initializer_list<int>& il, int = 0) noexcept
-      : value(il.size()) {
+      : value(static_cast<int>(il.size())) {
         ++alive; ++constructed; ++value_constructed;
     }
     template <bool Dummy = true, typename std::enable_if<Dummy && !Explicit, bool>::type = true>
-    explicit TestBase(std::initializer_list<int>& il, int = 0) noexcept : value(il.size()) {
+    explicit TestBase(std::initializer_list<int>& il, int = 0) noexcept : value(static_cast<int>(il.size())) {
         ++alive; ++constructed; ++value_constructed;
     }
     TestBase& operator=(int xvalue) noexcept {
@@ -135,9 +135,9 @@
     template <bool Dummy = true, typename std::enable_if<Dummy && !Explicit, bool>::type = true>
     constexpr ValueBase(int, int y) : value(y) {}
     template <bool Dummy = true, typename std::enable_if<Dummy && Explicit, bool>::type = true>
-    explicit constexpr ValueBase(std::initializer_list<int>& il, int = 0) : value(il.size()) {}
+    explicit constexpr ValueBase(std::initializer_list<int>& il, int = 0) : value(static_cast<int>(il.size())) {}
     template <bool Dummy = true, typename std::enable_if<Dummy && !Explicit, bool>::type = true>
-    constexpr ValueBase(std::initializer_list<int>& il, int = 0) : value(il.size()) {}
+    constexpr ValueBase(std::initializer_list<int>& il, int = 0) : value(static_cast<int>(il.size())) {}
     TEST_CONSTEXPR_CXX14 ValueBase& operator=(int xvalue) noexcept {
         value = xvalue;
         return *this;
@@ -193,9 +193,9 @@
     template <bool Dummy = true, typename std::enable_if<Dummy && !Explicit, bool>::type = true>
     constexpr TrivialValueBase(int, int y) : value(y) {}
     template <bool Dummy = true, typename std::enable_if<Dummy && Explicit, bool>::type = true>
-    explicit constexpr TrivialValueBase(std::initializer_list<int>& il, int = 0) : value(il.size()) {}
+    explicit constexpr TrivialValueBase(std::initializer_list<int>& il, int = 0) : value(static_cast<int>(il.size())) {}
     template <bool Dummy = true, typename std::enable_if<Dummy && !Explicit, bool>::type = true>
-    constexpr TrivialValueBase(std::initializer_list<int>& il, int = 0) : value(il.size()) {};
+    constexpr TrivialValueBase(std::initializer_list<int>& il, int = 0) : value(static_cast<int>(il.size())) {}
     int value;
 protected:
     constexpr TrivialValueBase() noexcept : value(0) {}
Index: test/std/utilities/optional/optional.specalg/make_optional_explicit_initializer_list.pass.cpp
===================================================================
--- test/std/utilities/optional/optional.specalg/make_optional_explicit_initializer_list.pass.cpp
+++ test/std/utilities/optional/optional.specalg/make_optional_explicit_initializer_list.pass.cpp
@@ -23,9 +23,9 @@
 struct TestT {
   int x;
   int size;
-  constexpr TestT(std::initializer_list<int> il) : x(*il.begin()), size(il.size()) {}
+  constexpr TestT(std::initializer_list<int> il) : x(*il.begin()), size(static_cast<int>(il.size())) {}
   constexpr TestT(std::initializer_list<int> il, const int*)
-      : x(*il.begin()), size(il.size()) {}
+      : x(*il.begin()), size(static_cast<int>(il.size())) {}
 };
 
 int main()


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D27268.79773.patch
Type: text/x-patch
Size: 4812 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20161130/376ec2ac/attachment.bin>


More information about the cfe-commits mailing list