'Clang-Tidy: Do not implicitly decay an array into a pointer' 当使用 std::forward 和 const char*

'Clang-Tidy: Do not implicitly decay an array into a pointer' when using std::forward and const char*

我不明白为什么 Clang-Tidy 在以下示例中会产生错误 Clang-Tidy: Do not implicitly decay an array into a pointer

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");

从错误中我了解到 "hello world" 作为类型 const char[11](或类似)的数组在 std::forward 期间衰减为类型 const char*。但是为什么以及如何修复此错误? std::make_shared<Foo>("hello world")std::forward 的用法非常相似并且确实有效。

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" 是一个 char const[12],当你 构造一个临时 std::string 来调用 Foo 的构造函数。

But why and how can I fix this error?

抑制该警告的方法是:

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

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

这很有趣:

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

这看起来像是伪造的诊断。我会在全球范围内禁用它,因为这个用例很常见。