Home > Back-end >  'Clang-Tidy: Do not implicitly decay an array into a pointer' when using std::forward and
'Clang-Tidy: Do not implicitly decay an array into a pointer' when using std::forward and

Time:05-04

I do not understand why Clang-Tidy produces the error Clang-Tidy: Do not implicitly decay an array into a pointer in the following example:

struct Foo {
  explicit Foo(std::string _identifier) : identifier(std::move(_identifier)) {}

  std::string identifier;
};

struct Bar {
  template<typename... Ts>
  explicit Bar(Ts &&...args) : foo(std::forward<Ts>(args)...) {} // <-- Error

  Foo foo;
};

Bar bar("hello world");

From the error I understand that "hello world" being an array of type const char[11] (or similar) is decayed to type const char* somewhere during std::forward. But why and how can I fix this error? std::make_shared<Foo>("hello world") is very similar regarding usage of std::forward and does work.

CodePudding user response:

This looks like a bogus diagnostic. I would disable it globally, since this usecase is so common.

CodePudding user response:

From the error I understand that "hello world" being an array of type const char[11] (or similar) is decayed to type const char* somewhere during std::forward

"hello world" is a char const[12] and decays to char const* when you construct a temporary std::string to call Foo's constructor.

But why and how can I fix this error?

A way to suppress that warning is:

Bar bar(std::string{ "hello world" });

std::make_shared<Foo>("hello world") is very similar regarding usage of std::forward and does work.

This is interesting:

Foo foo0("hello world"); // No warning
auto foo1 = std::make_shared<Foo>("hello world"); // No warning

Bar bar0("hello world"); // Warning
auto bar1 = std::make_shared<Bar>("hello world"); // Warning
  • Related