mardi 2 avril 2019

How to avoid warning about the narrow conversion in parameter pack expansion

When compiling the following code by using gcc/6.4.0, I got the narrow conversion warning warning: narrowing conversion of ‘dims#0’ from ‘int’ to ‘long unsigned int’ inside { } [-Wnarrowing] bool set_shape(Ts... dims) { return set_shape(ArrayRef<size_t>{dims...}); } ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~ Here the ArrayRef is llvm/ArrayRef.

class A {
  ...

  template <typename... Ts>
  void resize(Ts... dims) {
    bool size_changed = set_shape(dims...);
  }

  template <typename... Ts>
  bool set_shape(Ts... dims) { 
    return set_shape(ArrayRef<size_t>{dims...}); 
  }

  bool set_shape(ArrayRef<size_t> dims) { 
    return false;
  }
  ...
};

However, if I changed the code to:

  template <typename... Ts>
  bool set_shape(Ts... dims) { 
    return set_shape(ArrayRef<size_t>{static_cast<size_t>(dims)...}); 
  }

I got the error: invalid static_cast from type ‘hice::SmallVector<long unsigned int, 5u>’ to type ‘size_t {aka long unsigned int}’ bool set_shape(Ts... dims) { return set_shape(ArrayRef<size_t>{static_cast<size_t>(dims)...}); } ^~~~~~~~~~~~~~~~~~~~~~~~~ error: no matching function for call to ‘ArrayRef<long unsigned int>::ArrayRef(<brace-enclosed initializer list>)’ bool set_shape(Ts... dims) { return set_shape(ArrayRef<size_t>{static_cast<size_t>(dims)...}); } ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Aucun commentaire:

Enregistrer un commentaire