[PATCH] D27540: [libcxx] [test] Fix MSVC warning C4244 "conversion from 'X' to 'Y', possible loss of data", part 3/7.

Stephan T. Lavavej via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 8 12:21:16 PST 2016


STL_MSFT added a comment.

I checked, and Clang 3.8.0 behaves identically to C1XX in the pair scenario. Here's the test case:

  #include <utility>
  
  template <typename A, typename B> struct Pair {
      A a;
      B b;
      
      template <typename X, typename Y> Pair(X&& x, Y&& y) : a(std::forward<X>(x)), b(std::forward<Y>(y)) { }
  };
  
  int main() {
      Pair<short, short> p(11, 22);
      return p.a == 11 && p.b == 22 ? 0 : 1;
  }

With `-Wconversion`, this emits:

  prog.cc:7:62: warning: implicit conversion loses integer precision: 'int' to 'short' [-Wconversion]
      template <typename X, typename Y> Pair(X&& x, Y&& y) : a(std::forward<X>(x)), b(std::forward<Y>(y)) { }
                                                              ~^~~~~~~~~~~~~~~~~~
  prog.cc:11:24: note: in instantiation of function template specialization 'Pair<short, short>::Pair<int, int>' requested here
      Pair<short, short> p(11, 22);
                         ^
  prog.cc:7:85: warning: implicit conversion loses integer precision: 'int' to 'short' [-Wconversion]
      template <typename X, typename Y> Pair(X&& x, Y&& y) : a(std::forward<X>(x)), b(std::forward<Y>(y)) { }
                                                                                     ~^~~~~~~~~~~~~~~~~~

So, if you were compiling your tests with `-Wconversion` and not suppressing warnings in "system headers", you'd see this in libc++ too.


https://reviews.llvm.org/D27540





More information about the cfe-commits mailing list