Try to search your question here, if you can't find : Ask Any Question Now ?

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

HomeCategory: stackoverflowHow to avoid warning about the narrow conversion in parameter pack expansion
Avatarrupesh asked 5 months ago

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)...}); }
~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

1 Answers
Best Answer
AvatarArben answered 5 months ago
Your Answer

10 + 4 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com